Results 1 to 7 of 7
  1. #1
    cheeseberry is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Smile Finding road....

    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));
    			System.out.println(add[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();		
    				}
    				System.out.println(add[Integer.parseInt(aqueue.peek().toString())]);
    		}
    			
    }
    }

    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];
    	   public Node link;
    	   StackReferenceBased aStack = new StackReferenceBased();
    	   
    	   public Road(){
    		   m = 9;
    		   n = 9;
    		   link = null;  
    	   }
    
    	   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);
    			mark[i] = address[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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,565
    Blog Entries
    7
    Rep Power
    21

    Default Re: Finding road....

    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Finding road....

    finding road with Stack or Queue.
    Can you explain what that means and the steps a program needs to take to solve that problem?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    cheeseberry is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Finding road....

    Quote Originally Posted by Norm View Post
    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.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Finding road....

    road from P to Z.
    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?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    cheeseberry is offline Member
    Join Date
    May 2014
    Posts
    3
    Rep Power
    0

    Default Re: Finding road....

    Finding road....-df.png


    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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Finding road....

    Sorry, I don't know what the image has to do with a 2D array.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Access Road 0.7.3
    By java software in forum Java Software
    Replies: 0
    Last Post: 06-09-2013, 11:05 PM
  2. Road Tax issue
    By bleepblopblorg in forum New To Java
    Replies: 3
    Last Post: 09-25-2012, 10:11 AM
  3. Access Road 0.7
    By java software in forum Java Software
    Replies: 0
    Last Post: 03-18-2012, 05:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •