棧實現簡單的走迷宮 c++


在學習“數據結構”一書的時候看到這一道題, 為了展示棧的用法對迷宮做了以下限制

1.迷宮的四周都是不可通的,這樣就避免解決邊界問題

2.從(1,1)出發,終點為(8,8), 這里用10*10的迷宮為例子

走迷宮通常用的窮舉法,即從入口出發,沿着某一方向向前探索,如果能走通就繼續向前走,如果不能就原路返回換一個方向再繼續探索,直到找到重點或者終止探索。為了能回到原來的位置,比較理想的是用棧來實現,把探索過的位置壓棧,當走到不能再走的路徑的時候,退回原路換方向繼續探索。

如果當前位置是可通的就將其壓棧,並把當前位置指向下一個位置。 如果當前位置不可通,就順着來的方向退到前一個位置,然后沿其他方向探索,如果前一個位置的所有方向都探索了,就繼續往回走,重復上面的過程。默認探索方向是向右開始探索,沿逆時針方向變換探索方向。

算法可以簡單的用下面的語言描述:

設定當前位置的初始值為入口位置

do{

  若當前位置可通,

  則{

     把當前位置壓棧;

     如果當前位置是出口位置,結束程序;

     否則把當前位置設置為其右邊的位置

}

否者{

    若棧不空且四周均不通{

         刪除棧頂位置,直到找到一個相鄰位置可以通的位置

}

    若棧不空且棧頂位置周圍還有沒有探索過的方向,

  則設定當前位置為下一個沒有探索過的位置

}

}while(棧不空)

 

 

 1 #include<iostream>
 2 #include<stack>
 3 #include<string>
 4 using namespace std;
 5 /*
 6     function: find a path in maze
 7      authour: Lai XingYu
 8         date: 2018/05/10
 9 */
10 bool vis[11][11];//儲存每個位置的訪問情況,false 為沒有訪問, true表示訪問過 
11 
12 class point{
13 public:
14     int x;
15     int y;
16     int dir;
17     point(int a, int b,int d=1){
18         x = a;
19         y = b;
20         dir = d;
21     }
22     bool equal(point a){return x==a.x && y==a.y;}
23 
24 };
25 //每個位置未訪問過,並且是可以通過的 則該位置是“可通”的
26 bool pass(int maze[][10], point pos){
27     return !vis[pos.x][pos.y] && !maze[pos.x][pos.y];
28 }
29 //順時針找下一個未探索的位置
30 point next_position(point a, int dir){
31     if(dir==1) return point(a.x+1, a.y);
32     if(dir==2) return point(a.x, a.y+1);
33     if(dir==3) return point(a.x-1, a.y);
34     if(dir==4) return point(a.x, a.y-1);
35 }
36 
37 stack<point> maze_path(int maze[][10], point start, point end){
38     stack<point> path;
39     point current_position = start;
40     do{
41         if(pass(maze, current_position)){
42             vis[current_position.x][current_position.y] = true;//標記已訪問過的位置
43             point temp(current_position.x, current_position.y, 1);
44             path.push(temp);
45             if(current_position.equal(end)) return path;
46             current_position = next_position(current_position, 1);
47         }else{
48             point temp = path.top();
49             path.pop();//四個方向都探索過的位置,繼續往回走
50             while(temp.dir==4 && !path.empty()){
51                 temp = path.top();
52                 path.pop();
53             }
54             if(temp.dir<4){
55                 temp.dir++;
56                 path.push(temp);
57                 current_position = next_position(temp, temp.dir);
58             }
59         }
60     }while(path.size());
61     return path;
62 }
63 
64 int main(){//1表示該位置不能通過, 反之可以通過
65     int maze[][10]={{1,1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,1,0,0,0,1,0,1},{1,0,0,0,0,1,1,0,0,1},{1,0,1,1,1,0,0,0,0,1},
66     {1,0,0,0,1,0,0,0,0,1},{1,0,1,0,0,0,1,0,0,1},{1,0,1,1,1,0,1,1,0,1},{1,1,0,0,0,0,0,0,0,1},{1,1,1,1,1,1,1,1,1,1}};
67     point start(1,1), end(8,8);
68     stack<point> path = maze_path(maze, start, end);
69     while(path.size()) {
70         printf("(%d,%d) ", path.top().x, path.top().y);
71         path.pop();
72     }
73 return 0;}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM