I've written a method that fills a bitmap represented by a m x n matrix. What I'm trying to do is to push the initial pixel to a stack, then in a while loop pop an element from the stack, color it and push neighboring pixels if they are the same color as the initial color of the initial pixel.
public void fill(int x, int y, char c) {
char tempColor = this.bitmap[y - 1][x - 1];
Point currentPoint;
Stack<Point> fillStack = new Stack<Point>();
fillStack.push(new Point(x, y));
do {
currentPoint = fillStack.pop();
// System.out.println(currentPoint.x + " " + currentPoint.y);
// System.out.println("Current state of the stack:");
// for (Point p: fillStack)
// System.out.println(p.x + " " + p.y);
this.bitmap[currentPoint.y - 1][currentPoint.x - 1] = c;
if (currentPoint.y - 1 > 0 && this.bitmap[currentPoint.y - 2][currentPoint.x - 1] == tempColor) {
fillStack.push(new Point(x, y - 1));
// System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y - 1));
}
if (currentPoint.y - 1 < n - 1 && this.bitmap[currentPoint.y][currentPoint.x - 1] == tempColor) {
fillStack.push(new Point(x, y + 1));
// System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y + 1));
}
if (currentPoint.x - 1 > 0 && this.bitmap[currentPoint.y - 1][currentPoint.x - 2] == tempColor) {
fillStack.push(new Point(x - 1, y));
// System.out.println("Pushing " + (currentPoint.x - 1) + " " + currentPoint.y);
}
if (currentPoint.x - 1 < m - 1 && this.bitmap[currentPoint.y - 1][currentPoint.x] == tempColor) {
fillStack.push(new Point(x + 1, y));
// System.out.println("Pushing " + (currentPoint.x + 1) + " " + currentPoint.y);
}
} while (!fillStack.isEmpty());
}
}
But it doesn't work for a reason I can't seem to spot. The output (debugging lines uncommented) is as follows:
3 3
Current state of the stack:
Pushing 3 2
Pushing 3 4
Pushing 4 3
4 3
Current state of the stack:
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
... and it goes on like this in an endless loop. What can be the problem?
Your print statements say one thing, your code does another! ;)
for example:
fillStack.push(new Point(x, y - 1));
System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y - 1));
See if you can spot the difference...
Related
Here is the problem link
Approach:
My approach is to simply give every other node the chance to be an in-between node for every 2 pairs of vertices i and j.
Below is my code for Floyd Warshall algorithm:
class Solution{
public void shortest_distance(int[][] mat){
int N = mat.length;
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++ j){
for(int k = 0; k < N; ++k){
if(mat[i][k] != -1 && mat[k][j] != -1 && (mat[i][j] == -1 || mat[i][j] > mat[i][k] + mat[k][j])){
mat[i][j] = mat[i][k] + mat[k][j];
}
}
}
}
}
}
This gives wrong answer for the below case:
Input
12
0 4 2 1 2 9 4 8 -1 4 -1 -1
9 0 3 6 2 6 2 3 6 -1 -1 3
7 1 0 10 8 9 1 3 -1 7 -1 10
5 1 9 0 3 -1 1 10 7 1 -1 7
-1 5 1 4 0 2 10 4 10 6 4 5
7 8 3 7 5 0 5 1 3 5 7 2
6 -1 6 1 10 7 0 10 -1 -1 7 7
-1 3 2 7 4 -1 4 0 10 5 6 10
10 6 1 10 4 4 7 10 0 4 7 4
1 1 6 8 8 9 2 10 6 0 -1 3
5 9 3 -1 4 3 -1 -1 -1 3 0 1
2 2 8 6 2 4 4 3 -1 3 4 0
My output
0 2 2 1 2 4 2 5 7 2 6 5
5 0 3 3 2 4 2 3 6 4 6 3
6 1 0 2 3 5 1 3 7 3 7 4
2 1 4 0 3 5 1 4 7 1 7 4
6 2 1 3 0 2 2 3 5 4 4 4
4 4 3 5 4 0 4 1 3 5 6 2
3 2 5 1 4 6 0 5 8 2 7 5
6 3 2 4 4 6 3 0 9 5 6 6
5 2 1 3 4 4 2 4 0 4 7 4
1 1 3 2 3 5 2 4 6 0 7 3
3 3 3 4 3 3 4 4 6 3 0 1
2 2 3 3 2 4 4 3 7 3 4 0
Expected Output
0 2 2 1 2 4 2 5 7 2 6 5
5 0 3 3 2 4 2 3 6 4 6 3
4 1 0 2 3 5 1 3 7 3 7 4
2 1 4 0 3 5 1 4 7 1 7 4
5 2 1 3 0 2 2 3 5 4 4 4
4 4 3 5 4 0 4 1 3 5 6 2
3 2 5 1 4 6 0 5 8 2 7 5
6 3 2 4 4 6 3 0 9 5 6 6
5 2 1 3 4 4 2 4 0 4 7 4
1 1 3 2 3 5 2 4 6 0 7 3
3 3 3 4 3 3 4 4 6 3 0 1
2 2 3 3 2 4 4 3 7 3 4 0
Note: I have also observed that if I move the k loop out and make it the first loop, it works just fine. I am confused as to what is wrong with my current code.
It is all about states. This is more conceptual than any algorithmic implementation issue here.
As in your code,
for(i...)
for(j..)
for(k...)
mat[i][j] = mat[i][k] + mat[k][j];
The above states, for every pair of nodes i and j, check if any shortest path exists via every possible k. However, you are implicitly assuming that mat[i][k] and mat[k][j] are in already optimized states. This is incorrect as there is no real effort made in the code to bring them to that state.
Instead, you need to check for every pair of nodes i and j via only 1 value of k at a time. This is how you would be building the optimized states for every possible k one by one and the next one depending on the previous one. Hence, the below is correct version for this:
class Solution{
public void shortest_distance(int[][] mat){
int N = mat.length;
for(int k = 0; k < N; ++k){
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
if(mat[i][k] != -1 && mat[k][j] != -1 && (mat[i][j] == -1 || mat[i][j] > mat[i][k] + mat[k][j])){
mat[i][j] = mat[i][k] + mat[k][j];
}
}
}
}
}
}
Quoting from the Wiki.
The Floyd–Warshall algorithm compares all possible paths through the
graph between each pair of vertices. It is able to do this with
{\displaystyle \Theta (|V|^{3})}\Theta (|V|^{3}) comparisons in a
graph, even though there may be up to {\displaystyle \Omega
(|V|^{2})}{\displaystyle \Omega (|V|^{2})} edges in the graph, and
every combination of edges is tested. It does so by incrementally
improving an estimate on the shortest path between two vertices, until
the estimate is optimal.
Miscellaneous:
Although trivial, but I still think it is worth mentioning that the order of nodes used in the optimization process really doesn't matter. We simply need to shorten the distance(if exists and possible) node by node where every node is an intermediate candidate.
class Solution{
public void shortest_distance(int[][] mat){
int N = mat.length;
List<Integer> nodes = new ArrayList<>();
for(int i = 0; i < N; ++i) nodes.add(i);
Collections.shuffle(nodes);
for(int l = 0; l < nodes.size(); ++l){
int k = nodes.get(l);
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
if(mat[i][k] != -1 && mat[k][j] != -1 && (mat[i][j] == -1 || mat[i][j] > mat[i][k] + mat[k][j])){
mat[i][j] = mat[i][k] + mat[k][j];
}
}
}
}
}
}
With a set of 8*8 2d array that is already assigned with random 1 and 2 ,after finding the first 1 in the first row,and change it to 0, how could a recursion of method use to change all the 1 surround to 0(from all direction) and stop when it touch 2.
for example
1 2 2 1 2 2 1 1
1 1 2 2 1 2 1 1
2 1 2 1 1 2 1 1
2 1 2 1 2 1 1 1
2 1 2 2 2 2 1 2
2 1 2 2 1 2 2 2
2 2 1 1 1 2 1 2
1 1 2 1 2 1 2 1
to
0 2 2 1 2 2 1 1
0 0 2 2 1 2 1 1
2 0 2 1 1 2 1 1
2 0 2 1 2 1 1 1
2 0 2 2 2 2 1 2
2 0 2 2 0 2 2 2
2 2 0 0 0 2 0 2
0 0 2 0 2 0 2 0
This is very similar to minesweeper.
You would start with which ever index you please and then there are 8 points you could possibly check.
Top left
Top
Top Right
Right
Bottom Right
Bottom
Bottom Left
Left
You would then call your recursive function, making sure that each new index that you are checking is a valid index of the array (Numbers in between and including 0 and length - 1).
For example:
public void reveal(int index1, int index2) {
// Quit if number is a 2
if(array[index1][index2] == 2)
return;
else {
// Check for correct index before calling
...
// Then
reveal(index1-1,index2-1);
reveal(index1-1,index2);
reveal(index1-1,index2+1);
reveal(index1,index2+1);
reveal(index1+1,index2+1);
reveal(index1+1,index2);
reveal(index1+1,index2-1);
reveal(index1,index2-1);
}
}
I am working on a lab for a class where a user inputs a number and it recursively prints out a number pattern. For example,
The base case is if they enter 1, it will print: 1
If they enter 2 it will print: 1 2 1
If 3, it will print: 1 2 1 3 1 2 1
and then for something bigger, if they enter 7, it will print:
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
I'm a little stuck on what the number pattern is to be able to complete this problem. Does anyone have any ideas?
So you need to write a recursive function. Something of this form:
private String pattern(int num) {
// ...
}
The most important part is finding the right exit condition that should stop the recursion. In this case, that's when num == 1.
From the description, it looks like for a number k,
the output is pattern(k - 1) + k + pattern(k - 1).
I already spoiled too much.
You might need to improve the efficiency of this.
For example, realize that you don't need to run pattern(k - 1) twice,
it's enough to do it once.
I'm a little stuck on what the number pattern is to be able to
complete this problem.
Lets try to analyse the sequence using some function f
f(1) = 1 (Total digits = 1)
f(2) = 1 2 1 ( Total digits = 3)
f(3) = 121 3 121 (Total digits = 7)
f(4) = 1213121 4 1213121 (Total digits = 15)
f(5) = 121312141213121 5 121312141213121 (Total digits = 31)
So as you can observe total digits sequence looks like 1,3,7,15,31,....2^n-1
Now we can express this logic as mentioned below(Note : in order to help you to better understand how the program works i am printing sequence at every level)
public class SequenceGenerator {
public static void main(String[] args) {
generate(7);
}
static void generate(int depth) {
recursiveGenerator(1, null, depth);
}
static void recursiveGenerator(int num, String prev, int limit) {
if (num <= limit) {
if (prev != null) {
System.out.println();
}
if (prev != null) {
System.out.printf("%s %d %s", prev, num, prev);
} else {
prev = "";
System.out.printf("%d", num);
}
if (prev.equals("")) {
prev += num + prev;
} else {
prev += " " + num + " " + prev;
}
recursiveGenerator(++num, prev, limit);
}
}
}
Outputs
1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
So I have a simple program that creates a LinkedList array of given size n, with each value in the list representing a new separate LinkedList data Structure.
public class Graph {
public final LinkedList[] graph;
public Graph(int n){
graph = new LinkedList[n];
for (int i=0; i<n; i++){
graph[i] = new LinkedList();
}
}
public void addEdge(int x, int y){
graph[x].addFirst(y);
graph[y].addFirst(x);
}
For some reason, however, when I call the addEdge() method with two int values, instead of adding them to the specific called LinkedList in graph[], it adds them to every LinkedList in graph[].
What is the problem here?
Edit:*
public void addEdge(int x, int y){
graph[x].addFirst(y);
graph[y].addFirst(x);
for (int i=0; i<graph.length; i++){
Node tmp = graph[i].first;
System.out.println(i + ":");
while (tmp != null){
System.out.print(tmp.name + " ");
tmp = tmp.Rnext;
}
System.out.println();
}
System.out.println();
}
public class Test {
public static void main(String[] args) {
Graph myGraph1 = new Graph(8);
myGraph1.addEdge(1, 2);
myGraph1.addEdge(1, 7);
myGraph1.addEdge(1, 4);
myGraph1.addEdge(2, 5);
myGraph1.addEdge(2, 6);
myGraph1.addEdge(6, 3);
myGraph1.addEdge(3, 8);
myGraph1.addEdge(5, 7);
}
}
Here is the output of graph:
0:
1 2
1:
1 2
2:
1 2
3:
1 2
4:
1 2
5:
1 2
6:
1 2
7:
1 2
0:
1 7 1 2
1:
1 7 1 2
2:
1 7 1 2
3:
1 7 1 2
4:
1 7 1 2
5:
1 7 1 2
6:
1 7 1 2
7:
1 7 1 2
0:
1 4 1 7 1 2
1:
1 4 1 7 1 2
2:
1 4 1 7 1 2
3:
1 4 1 7 1 2
4:
1 4 1 7 1 2
5:
1 4 1 7 1 2
6:
1 4 1 7 1 2
7:
1 4 1 7 1 2
0:
2 5 1 4 1 7 1 2
1:
2 5 1 4 1 7 1 2
2:
2 5 1 4 1 7 1 2
3:
2 5 1 4 1 7 1 2
4:
2 5 1 4 1 7 1 2
5:
2 5 1 4 1 7 1 2
6:
2 5 1 4 1 7 1 2
7:
2 5 1 4 1 7 1 2
0:
2 6 2 5 1 4 1 7 1 2
1:
2 6 2 5 1 4 1 7 1 2
2:
2 6 2 5 1 4 1 7 1 2
3:
2 6 2 5 1 4 1 7 1 2
4:
2 6 2 5 1 4 1 7 1 2
5:
2 6 2 5 1 4 1 7 1 2
6:
2 6 2 5 1 4 1 7 1 2
7:
2 6 2 5 1 4 1 7 1 2
0:
6 3 2 6 2 5 1 4 1 7 1 2
1:
6 3 2 6 2 5 1 4 1 7 1 2
2:
6 3 2 6 2 5 1 4 1 7 1 2
3:
6 3 2 6 2 5 1 4 1 7 1 2
4:
6 3 2 6 2 5 1 4 1 7 1 2
5:
6 3 2 6 2 5 1 4 1 7 1 2
6:
6 3 2 6 2 5 1 4 1 7 1 2
7:
6 3 2 6 2 5 1 4 1 7 1 2
Here is the LinkedList and Node Class I am using:
import java.util.NoSuchElementException;
public class LinkedList {
public static Node first;
public LinkedList(){
first = null;
}
// Returns true if the list is empty
public boolean isEmpty(){
return first == null;
}
// Inserts a new node at the beginning of this list.
public void addFirst(int name){
first = new Node(name, first);
}
public boolean findData(int d){
if(first == null) throw new NoSuchElementException();
Node tmp = first;
while (tmp != null) {
if (tmp.name == d) return true;
tmp = tmp.Rnext;
} return false;
}
}
public class Node {
public int name;
public Node Rnext;
public Node(){
name = 0;
Rnext = null;
}
public Node(int n, Node r){
this.name = n;
this.Rnext = r;
}
}
public static Node first;
This is the problem. Every single LinkedList you make is sharing the same Node, so they're all effectively the same list.
Don't use static for instance variables.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
___________1
__________1 2 1
_________1 2 3 2 1
________1 2 3 4 3 2 1
______1 2 3 4 5 4 3 2 1
_____1 2 3 4 4 4 4 4 3 2 1
___1 2 3 3 3 3 3 3 3 3 3 2 1
__1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
_1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I would like to create this pyramid using java? Any suggestion?
This should do it:
public class Tower {
public static void main(String[] args) {
System.out.println(" 1 ");
System.out.println(" 1 2 1 ");
System.out.println(" 1 2 3 2 1 ");
System.out.println(" 1 2 3 4 3 2 1 ");
System.out.println(" 1 2 3 4 5 4 3 2 1 ");
System.out.println(" 1 2 3 4 4 4 4 4 3 2 1 ");
System.out.println(" 1 2 3 3 3 3 3 3 3 3 3 2 1 ");
System.out.println(" 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 ");
System.out.println(" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ");
}
}
Try using a mono-spaced font like courier.
This will surve the purpose. You can change the number 5 to another number other than 5. eg. 1,2,3,.. , 6,8
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
for(int i = 5; i > 0; i-- ){
wrapWithNumber(list, i);
}
for (String string : editListToBeInTriangleShape(list)) {
System.out.println(string);
};
}
/**
* Wrap the number strings in the llist with a perticular number.
* #param list list of Strings
* #param ba number which need to wrapp the list with.
*/
private void wrapWithNumber(List<String> list, final int ba) {
list.add(0, String.format("%d",ba));
for (int i = 1; i < list.size(); i++) {
String newformat = "%1$d " + list.get(i) + " %1$d";
list.remove(list.get(i));
list.add(i,String.format(newformat, ba));
}
String lastFormat = "%1$d";
for(int i = 0; i < 2 * list.size();i++){
lastFormat += " %1$d";
}
if(list.size() != 1) {
list.add(String.format(lastFormat, ba));
}
}
/**
* Arrage the Strings in the list in triangular manner.
* #param list list of Strings.
*/
private List<String> editListToBeInTriangleShape(final List<String> list) {
final List<String> returnList = new LinkedList<String>();
for (int i = list.size(); i > 0; i--) {
String s = list.get(list.size()-i);
int possition = list.size()*2 + s.length()/2;
returnList.add(String.format("%"+possition+"s", s));
}
return returnList;
}
out put of this :
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 4 4 4 3 2 1
1 2 3 3 3 3 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I would suggest a series of for loops.