Results 1 to 2 of 2
  1. #1
    Ing. Balderas is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Question Question with a Tree and Breadth First Search

    PHP Code:
    // Breadth First Search Usage in the common Eight Puzzle Problem.
    import java.util.*;
    
    class EightPuzzle {
    	
    	Queue<String> q = new LinkedList<String>();    // Use of Queue Implemented using LinkedList for Storing All the Nodes in BFS.
    	Map<String,Integer> map = new HashMap<String, Integer>(); // HashMap is used to ignore repeated nodes
    	
    	public static void main(String args[]){
    		
    		
    		String str="087465132";					// Input the Board State as a String with 0 as the Blank Space 
    
    		EightPuzzle e = new EightPuzzle(); 		// New Instance of the EightPuzzle
    		e.add(str,0); 							// Add the Initial State
    		
    		while(e.q.peek()!=null){
    			
    			e.up(e.q.peek());					// Move the blank space up and add new state to queue
    			e.down(e.q.peek());					// Move the blank space down 
    			e.left(e.q.peek());					// Move left
    			e.right(e.q.remove());				// Move right and remove the current node from Queue
    		}
    		System.out.println("Solution doesn't exist");
    	}
    	
    	//Add method to add the new string to the Map and Queue
    	void add(String str,int n){
    		if(!map.containsKey(str)){
    			map.put(str,n);
    			q.add(str);
    		}
    	}
    	
    	/* Each of the Methods below Takes the Current State of Board as String. Then the operation to move the blank space is done if possible.
    	      After that the new string is added to the map and queue.If it is the Goal State then the Program Terminates.
    	*/
    	void up(String str){
    		int a = str.indexOf("0");
    		if(a>2){
    			String s = str.substring(0,a-3)+"0"+str.substring(a-2,a)+str.charAt(a-3)+str.substring(a+1);
    			add(s,map.get(str)+1);
    			if(s.equals("123456780")) {
    				System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
    				System.exit(0);
    			}
    		}
    	}
    	void down(String str){
    		int a = str.indexOf("0");
    		if(a<6){
    			String s = str.substring(0,a)+str.substring(a+3,a+4)+str.substring(a+1,a+3)+"0"+str.substring(a+4);
    			add(s,map.get(str)+1);
    			if(s.equals("123456780")) {
    				System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
    				System.exit(0);
    			}
    		}
    	}
    	void left(String str){
    		int a = str.indexOf("0");
    		if(a!=0 && a!=3 && a!=6){
    			String s = str.substring(0,a-1)+"0"+str.charAt(a-1)+str.substring(a+1);
    			add(s,map.get(str)+1);
    			if(s.equals("123456780")) {
    				System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
    				System.exit(0);
    			}
    		}
    	}
    	void right(String str){
    		int a = str.indexOf("0");
    		if(a!=2 && a!=5 && a!=8){
    			String s = str.substring(0,a)+str.charAt(a+1)+"0"+str.substring(a+2);
    			add(s,map.get(str)+1);
    			if(s.equals("123456780")) {
    				System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
    				System.exit(0);
    			}
    		}
    	}
    }
    //Coded by Neo ...
    I Found this code, thats is excellent... credits to Neo...

    But i want to see the steps to arrive to the correct answers

    i show me the level, but i want to see.. the movements..Example: UP, DOWN, DOWN , LEFT , RIGHT...ETC..

    can someone give me some ideas...

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2010, 08:40 AM
  2. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 02:42 AM
  3. Binary Search Tree
    By anmadie in forum New To Java
    Replies: 5
    Last Post: 11-17-2009, 03:39 AM
  4. Graph - Breadth First search for searching a node ?
    By JavaInLove in forum Advanced Java
    Replies: 1
    Last Post: 10-06-2008, 11:17 PM
  5. Breadth First Search and Friends!
    By nokomis in forum New To Java
    Replies: 0
    Last Post: 11-09-2007, 09:16 PM

Posting Permissions

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