1 Importjava.util.LinkedList;2 ImportJava.util.Queue;3 ImportJava.util.Stack;4 5 Public classBFS6 {7 Private Static Final intm=3;//Representative Row8 Private Static Final intn=3;//represents a column9 Ten intDestx=1; One intDesty=0; A - //int[][] maze={{0,0,1,1},{0,0,0,1},{1,1,0,1},{0,0,0,0}};//Maze Layout, 1 means obstacle - int[] maze={{0,0,0},{0,1,0},{0,0,0}};//Maze Layout, 1 means obstacle the int[] visit=New intM [N];//whether the tag has been visited - int[] steparr={{-1,0},{1,0},{0,-1},{0,1}};//indicates the direction of walking: up or down - - + - classNode + { A intx, y;//represents the coordinates of the current position at intStep; - intPrex,prey; - -Node (intXintYintPreX,intPreY,intStep) - { - This. x=x;//represents the horizontal axis of the current position in This. y=y;//represents the ordinate of the current position - This. Prex=prex;//represents the horizontal axis of the previous step in the current position to This. Prey=prey;//An ordinate that represents the previous step in the current position + This. Step=step;//It means walking away. - } the } * $ Public BooleanBFS ()Panax Notoginseng { -Node node=NewNode (0,0,-1,-1,0);//Start Position theQueue<node> queue=NewLinkedlist<node> ();//the LinkedList class implements the queue interface +Stack<node> stack=NewStack<node>(); A theQueue.offer (node);//Insert Node + - while(!queue.isempty ()) $ { $Node Head=queue.poll ();//Launch Queue Header - -Stack.push (head);//used to save paths theVISIT[HEAD.X][HEAD.Y] = 1;//indicates that the current location has been accessed to prevent two coordinates from being accessed in a loop - for(inti = 0; I < 4; i++) {//4 here means 4 directions.Wuyi intx = head.x + steparr[i][0]; the inty = head.y + steparr[i][1]; - //Exit Wu if(x ==destx && y = = Desty && maze[x][y] = = 0 && visit[x][y] = = 0) {//means to find a destination - //Print Path AboutNode top =Stack.pop (); $System.out.println ("Steps:" + (Top.step + 1));//The path shown here is the shortest path, because it is the queue that holds every possible -SYSTEM.OUT.PRINTLN ("The Path:"); -System.out.println ((M-1) + "," + (N-1)); -System.out.println (top.x + "," +top.y); A intPreX =Top.prex; + intPreY =Top.prey; the while(!Stack.isempty ()) { -top =Stack.pop (); $ if(PreX = = top.x && PreY = = top.y) {//because every possible time is saved in the queue, it needs to be validated with the previously saved point theSystem.out.println (PreX + "," +PreY); thePreX =Top.prex; thePreY =Top.prey; the } - in } the return true; the } About //BFS the if(x >= 0 && x < M && y >= 0 && y < N &&maze[x][y] = 0 && Visit[x][y] = = 0){ theNode NewNode =NewNode (x, Y, head.x, Head.y, Head.step + 1); the Queue.offer (NewNode); + } - } the Bayi } the return false; the } - - the Public Static voidMain (string[] args) the { theBFS b=NewBFS (); the if(B.BFS () = =false) - { theSystem.out.println ("fail!"); the } the }94 the the the 98}
The shortest path of labyrinth can be obtained by using BFS.
If you use DFS, you can only judge if you can get out of the maze, and not know whether the path is the shortest path.
Maze problem (BFS)