POJ - 3984 迷宮問題 (搜索)

Problem Description

定義一個二維數組:

int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};

它表示一個迷宮,其中的1表示牆壁,0表示能夠走的路,只能橫着走或豎着走,不能斜着走,要求編程序找出從左上角到右下角的最短路線。

Input

一個5 × 5的二維數組,表示一個迷宮。數據保證有惟一解。

Output

左上角到右下角的最短路徑,格式如樣例所示。

Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

也是kuangbin搜索專題裏面的,提及這道題,也是滿滿的惡意,先看圖吧

整整花了一個小時去找到底哪裏PE了。ios

題目思路很明確,BFS或者DFS均可以,但其實這個題目不必DFS,簡單BFS標記一下前驅就好了,何爲前驅,就是說你走到了下一步你上一步 是從哪裏走來的,而後用優先隊列保證每次優先走距離右下角最近的路。那麼問題來了,如何輸出,由於咱們存的是前驅,因此能夠先把全部前驅 入棧,再依次出棧輸出就好了,但最開始我想到了另外一種更好的方法,由於我發現編程

ostringstream outt;
outt << 2 << 1 << 3;

string s = outt.str();
reverse(s.begin(), s.end());
cout << s << endl;

利用ostringstream流,最後倒過來就能夠實現直接順序輸出了,提交PE。找了半天發現是<<endl;倒序後變成先輸出了,那麼我第一個不加endl,再次提交PE、PE、PE、PE。到這裏我以爲多是ostringstream影響緩衝區,不能這樣,改爲棧模擬,仍是PE =7=,直到最後我發現,(a, b)中間 逗號後面 有個空格 /微笑/微笑。而後改了兩種方法都AC了;數組

 

AC代碼spa

  1 #include <iostream>
  2 #include <string>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <sstream>
  6 #include <iomanip>
  7 #include <map>
  8 #include <stack>
  9 #include <deque>
 10 #include <queue>
 11 #include <vector>
 12 #include <set>
 13 #include <list>
 14 #include <cstring>
 15 #include <cctype>
 16 #include <algorithm>
 17 #include <iterator>
 18 #include <cmath>
 19 #include <bitset>
 20 #include <ctime>
 21 #include <fstream>
 22 #include <limits.h>
 23 #include <numeric>
 24 
 25 using namespace std;
 26 
 27 #define F first
 28 #define S second
 29 #define mian main
 30 #define ture true
 31 
 32 #define MAXN 1000000+5
 33 #define MOD 1000000007
 34 #define PI (acos(-1.0))
 35 #define EPS 1e-6
 36 #define MMT(s) memset(s, 0, sizeof s)
 37 typedef unsigned long long ull;
 38 typedef long long ll;
 39 typedef double db;
 40 typedef long double ldb;
 41 typedef stringstream sstm;
 42 const int INF = 0x3f3f3f3f;
 43 
 44 int mp[5][5],vis[5][5];
 45 int fx[4][2] = {1,0,-1,0,0,-1,0,1};
 46 vector< pair< int, pair<int,int> > >bj(40); //記錄前驅和位置
 47 ostringstream outt;
 48 class cmp{  //優先隊列使得曼哈頓距離小的優先出隊
 49 public:
 50     bool operator() (const pair<int,int>a,const pair<int,int>b) const{
 51         int ax = 4 - a.F + 4 - a.S;
 52         int bx = 4 - b.F + 4 - b.S;
 53         return ax > bx;
 54     }
 55 };
 56 
 57 void bfs(){
 58     priority_queue< pair<int,int>,vector< pair<int,int> >, cmp >q;
 59     //queue< pair<int,int> >q;
 60     q.push(make_pair(0,0));
 61     vis[0][0] = 1;
 62     while(!q.empty()){
 63         pair<int,int>nx = q.top();
 64         q.pop();
 65         //cout << nx.F << " " << nx.S << endl;
 66         if(8 - (nx.F + nx.S) == 1){
 67             bj[24].F = nx.F*5+nx.S;
 68             bj[24].S.F = 4, bj[24].S.S = 4;
 69             break;
 70         }
 71 
 72         for(int i = 0; i < 4; i++){
 73             int nxx = nx.F + fx[i][0];
 74             int nxy = nx.S + fx[i][1];
 75             if(nxx < 0 || nxx > 4 || nxy < 0 || nxy > 4 || mp[nxx][nxy] == 1 || vis[nxx][nxy])
 76                 continue;
 77             vis[nxx][nxy] = 1;
 78             q.push(make_pair(nxx,nxy));
 79             bj[nxx*5+nxy].F = nx.F*5+nx.S;
 80             bj[nxx*5+nxy].S.F = nxx, bj[nxx*5+nxy].S.S = nxy;
 81         }
 82     }
 83     int nex = 24;
 84     /* //這是用棧模擬的方式
 85     stack<int>p;
 86     while(nex){
 87         p.push(nex);
 88         nex = bj[nex].F;
 89     }
 90     p.push(0);
 91     while(!p.empty()){
 92         nex = p.top();
 93         p.pop();
 94         cout << "(" << bj[nex].S.F << ", " << bj[nex].S.S << ")"<< endl;
 95     }
 96     */
 97     while(1){  //反向輸出到ostringstream中
 98         //cout << nex << endl;
 99         if(nex == 0){
100             outt << ")" << bj[nex].S.S << " ," << bj[nex].S.F << "(";
101             break;
102         }
103         outt << ")" << bj[nex].S.S << " ," << bj[nex].S.F << "(" << "\n";
104         nex = bj[nex].F;
105     }
106 
107 }
108 
109 int main(){
110     ios_base::sync_with_stdio(false);
111     cout.tie(0);
112     cin.tie(0);
113     fill(vis[0],vis[0]+5*5,0);
114     for(int i = 0; i < 5; i++){
115         for(int j = 0; j < 5; j++){
116             cin>>mp[i][j];
117         }
118     }
119     bfs();
120     string s = outt.str();
121     reverse(s.begin(),s.end());  //再次逆序
122     cout << s << endl;
123 
124     return 0;
125 }

其餘就是一個簡單的BFS,值得注意就是優先隊列的使用,固然用DFS也行 ,並且DFS就不須要這麼多繁雜的逆序了,直接記錄從起點到終點的路徑輸出就行了。code

相關文章
相關標籤/搜索