迷宫问题(bfs)

 1 import java.util.LinkedList;
 2 import java.util.Queue;
 3 import java.util.Stack;
 4 
 5 public class BFS
 6 {
 7     private static final int M=3;//代表行
 8     private static final int N=3;//代表列
 9     
10     int destX=1;
11     int destY=0;
12     
13     //int[][] maze={{0,0,1,1},{0,0,0,1},{1,1,0,1},{0,0,0,0}};//迷宫布局,1表示障碍物
14     int[][] maze={{0,0,0},{0,1,0},{0,0,0}};//迷宫布局,1表示障碍物
15     int[][] visit=new int[M][N];//标记是否已经访问过
16     int[][] stepArr={{-1,0},{1,0},{0,-1},{0,1}};//表示走路的方向:上下左右
17     
18     
19     
20     class Node 
21     {
22         int x,y;//表示当前位置的坐标
23         int step;
24         int preX,preY;
25         
26         Node(int x,int y,int preX,int preY,int step)
27         {
28             this.x=x;//表示当前位置的横坐标
29             this.y=y;//表示当前位置的纵坐标
30             this.preX=preX;//表示当前位置前一步的横坐标
31             this.preY=preY;//表示当前位置前一步的纵坐标
32             this.step=step;//表示行走的路程
33         }        
34     }
35     
36     public boolean bfs()
37     {
38         Node node=new Node(0,0,-1,-1,0);//起始位置
39         Queue<Node> queue=new LinkedList<Node>();//LinkedList类实现了Queue接口
40         Stack<Node> stack=new Stack<Node>();
41         
42         queue.offer(node);//插入node
43         
44         while(!queue.isEmpty())
45         {
46             Node head=queue.poll();//推出队列头
47             
48             stack.push(head); //用于保存路径  
49             visit[head.x][head.y] = 1;  //表示当前位置已被访问,防止两个坐标循环访问
50             for(int i = 0; i < 4; i++){  //这里的4表示4张方向
51                 int x = head.x + stepArr[i][0];  
52                 int y = head.y + stepArr[i][1];  
53                 //exit  
54                 if(x ==destX && y == destY && maze[x][y] == 0 && visit[x][y] == 0){ //表示找到目的地 
55                     //打印路径  
56                     Node top = stack.pop();  
57                     System.out.println("steps:" + (top.step + 1)); //此处得出的路径是最短路径,因为是queue中保存了每一种可能
58                     System.out.println("the path:");  
59                     System.out.println((M - 1) + "," + (N - 1));  
60                     System.out.println(top.x + "," + top.y);  
61                     int preX = top.preX;  
62                     int preY = top.preY;  
63                     while(!stack.isEmpty()){  
64                         top = stack.pop();  
65                         if (preX == top.x && preY == top.y) { // 因为队列中保存了每一次的各种可能,故需要与之前保存的点进行验证
66                             System.out.println(preX + "," + preY);  
67                             preX = top.preX;  
68                             preY = top.preY;  
69                         }  
70                           
71                     }  
72                     return true;  
73                 }  
74                 //bfs  
75                 if(x >= 0 && x < M && y >= 0 && y < N &&maze[x][y] == 0 && visit[x][y] == 0){  
76                     Node newNode = new Node(x, y, head.x, head.y, head.step + 1);  
77                     queue.offer(newNode);  
78                 }  
79             }
80                         
81         }        
82         return false;  
83     }
84     
85     
86     public static void main(String[] args)
87     {
88         BFS b=new BFS();
89         if (b.bfs()==false)
90         {
91             System.out.println("Fail!");
92         }
93     }
94     
95     
96     
97     
98 }

利用bfs可求出迷宫的最短路径。

如果使用dfs的话只能判断能否走出迷宫,并不能知道所走路径是否为最短路径。

原文地址:https://www.cnblogs.com/xh0102/p/5768397.html