• 05-02-2014, 06:14 AM
cheeseberry
Hi :)
I have to make code about finding road with Stack or Queue.

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

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.
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?
• 05-02-2014, 08:46 AM
JosAH
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
• 05-02-2014, 01:34 PM
Norm
Quote:

finding road with Stack or Queue.
Can you explain what that means and the steps a program needs to take to solve that problem?
• 05-02-2014, 04:28 PM
cheeseberry
Quote:

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.
• 05-02-2014, 04:40 PM
Norm
Quote:

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?
• 05-02-2014, 04:44 PM
cheeseberry