1. Member
Join Date
May 2014
Posts
3
Rep Power
0

Hi :)
I have to make code about finding road with Stack or Queue.

At first, I tried to make it in this way...

Java Code:
```//public class QueueMain {
public static void main(String[] args){
int [][] arr = {{0, 0, 1, 0, 0, 1, 0, 0, 0},  //p
{0, 0, 0, 0, 0, 0, 1, 0, 0},//q
{0, 0, 0, 0, 0, 0, 1, 0, 0},//r
{0, 0, 0, 0, 1, 0, 0, 0, 0},//s
{0, 0, 0, 0, 0, 1, 0, 0, 0},//t
{0, 0, 0, 1, 0, 0, 0, 1, 0},//w
{0, 0, 0, 0, 0, 0, 0, 0, 0},//x
{0, 0, 1, 0, 0, 0, 0, 0, 1},//y
{0, 0, 0, 0, 0, 0, 0, 0, 0}};//z
String [] add = {"P", "Q", "R", "S", "T", "W", "X", "Y", "Z"};
int originnumber = 0;
int destinationnumber = 8;
Queue aqueue = new Queue();
aqueue.enqueue(new Integer(originnumber));
while(!aqueue.isEmpty() && aqueue.peek() != new Integer(destinationnumber)){
int m = Integer.parseInt(aqueue.peek().toString());
int n = 0;
while(Integer.parseInt(aqueue.peek().toString()) == m &&  n < 9){
if(arr[m][n] == 0)
n++;
else{
aqueue.enqueue(new Integer(n));
arr[m][n] = 0;

}
for(int i =0; i < 9; i++){
arr[i][m] = 0;
}

if(n == 9)	aqueue.dequeue();
}
}

}
}```

But the professor said there could be other ways...
I tried for many hours and days... But I couldn't solve it. I made a new Code as below.
Java Code:
```//   int [][] arr = {{0, 0, 1, 0, 0, 1, 0, 0, 0},  //p
{0, 0, 0, 0, 0, 0, 1, 0, 0},//q
{0, 0, 0, 0, 0, 0, 1, 0, 0},//r
{0, 0, 0, 0, 1, 0, 0, 0, 0},//s
{0, 0, 0, 0, 0, 1, 0, 0, 0},//t
{0, 0, 0, 1, 0, 0, 0, 1, 0},//w
{0, 0, 0, 0, 0, 0, 0, 0, 0},//x
{0, 0, 1, 0, 0, 0, 0, 0, 1},//y
{0, 0, 0, 0, 0, 0, 0, 0, 0}};//z
String [] address = {"P", "Q", "R", "S", "T", "W", "X", "Y", "Z"};
public int m, n;
String [] mark = new String[10];
StackReferenceBased aStack = new StackReferenceBased();

m = 9;
n = 9;
}

void path(){
int m = 0;
int n = 0;
mark[0] = "P";

aStack.push(0, 0);

while(n<9){
if(arr[m][n]==0){
n++;
if(n==9)
aStack.pop();
}

else{
int i=1;
aStack.push(m, n);
m = m+1;
n=0;
i++;
}

}
System.out.println(mark.toString());

}

}```

But it doesn't work at all. I tried to put visited address into array<mark>... but it didn't work.
You guys could guess what the problem is?
Last edited by JosAH; 05-02-2014 at 08:41 AM. Reason: added [code] ... [/code] tags

Have you ever read something about 'autoboxing' and 'generics'? If not, please do so, because your code is full of violations against those features; I don't even know where to start ...

kind regards,

Jos

finding road with Stack or Queue.
Can you explain what that means and the steps a program needs to take to solve that problem?

4. Member
Join Date
May 2014
Posts
3
Rep Power
0

Originally Posted by Norm
Can you explain what that means and the steps a program needs to take to solve that problem?
We have to find road from P to Z.
'1' means the way that we can go. For example, we can go P->R since ([0,3]=1)
'0' means the way that we can't go.
And, if there's no way , we have to pop().
We can't go the place we have already visited.

Are P and Z objects with x and y values. What direction does the road go? Up, down, left or right only. Not in the diagonal directions.
What data is pushed and popped from the queue?

6. Member
Join Date
May 2014
Posts
3
Rep Power
0

In Stack, we start at 'P'.
then, P->R->X. There's no way from X. So the pop().
then, P->W->S->T. If T->W, this means we have to visit W again. It's not permitted, so pop().
So, the final answer becomes P->W->Y->Z

Sorry, I don't know what the image has to do with a 2D array.