cdoj1088-王之迷宮 (三維迷宮最短路徑)【BFS】

來源:互聯網
上載者:User

標籤:

http://acm.uestc.edu.cn/#/problem/show/1088

王之迷宮Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)Submit Status

王被困在了一個3維的迷宮中,他很想逃離這個迷宮回去當學霸,你能協助他麼? 由於王很仁慈,他悄悄地告訴你,本題讀入迷宮的每一行時,要用scanf("%s"...) ......

Input

多組測試資料,對於每組測試資料,有三個整數 L,R,C(0<l,r,c≤30)。

L代表迷宮的高度,R和C分別代表每一層的行和列。

接下來是L個R×C的矩陣,矩陣包含4種字元(S,E,.,#),S代錶王的初始位置,E代表出口,#代表障礙。.代表能通過的地方。

每一層之後有一個空行。

當L=R=C=0時,輸入中斷。

Output

如果可以逃離迷宮,按下列格式輸出最短時間:

Escaped in x minute(s). (x表示逃離迷宮的最短時間, 走一步花費一昏鐘)

否則,輸出:

Trapped!

Sample input and output
Sample Input Sample Output
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Escaped in 11 minute(s).Trapped!

 

 

題解:三維迷宮,很簡單的bfs。一開始wa了一發,後來發現是因為oj資料不規範,不能用getchar去消除空白符,得用%s過濾空白符。一方面有些抱怨oj資料坑爹,一方面總結教訓,要有勇氣敢於懷疑平台資料的正確性,改善自己程式的健壯性。

代碼:

 1 #include <fstream> 2 #include <iostream> 3 #include <cstdio> 4 #include <vector> 5 #include <cstdlib> 6 #include <cstring> 7 #include <algorithm> 8 #include <queue> 9 10 using namespace std;11 12 struct node{13     int x,y,z,step;14 }tmp;15 16 const int N=35;17 int l,r,c,x1,y1_,z1;18 queue<node> pq;19 char a[N][N][N];20 bool b[N][N][N];21 int co[3][6]={{0,0,0,0,1,-1},{0,0,1,-1,0,0},{1,-1,0,0,0,0}};22 23 inline bool check(int x,int y,int z);24 int bfs();25 26 int main()27 {28     //freopen("D:\\input.in","r",stdin);29     //freopen("D:\\output.out","w",stdout);30     while(scanf("%d%d%d",&l,&r,&c)&&(l|r|c)){31         bool bo=1;32         for(int i=0;i<l;i++){33             for(int j=0;j<r;j++){34                 scanf("%s",a[i][j]);35                 if(bo)36                     for(int z=0;z<c;z++){37                         if(a[i][j][z]==‘S‘){38                             x1=i,y1_=j,z1=z;39                             bo=0;40                             break;41                         }42                     }43             }44         }45         int ans=bfs();46         if(ans==-1) puts("Trapped!");47         else    printf("Escaped in %d minute(s).\n",ans);48     }49     return 0;50 }51 inline bool check(int x,int y,int z){52     return x>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c&&a[x][y][z]!=‘#‘&&b[x][y][z]==0;53 }54 int bfs(){55     memset(b,0,sizeof(b));56     while(!pq.empty()) pq.pop();57     tmp.step=0;58     tmp.x=x1;59     tmp.y=y1_;60     tmp.z=z1;61     pq.push(tmp);62     b[x1][y1_][z1]=1;63     while(!pq.empty()){64         tmp=pq.front();65         pq.pop();66         int x=tmp.x,y=tmp.y,z=tmp.z;67         int tx,ty,tz,ts=tmp.step+1;68         for(int i=0;i<6;i++){69             tx=x+co[0][i];70             ty=y+co[1][i];71             tz=z+co[2][i];72             if(check(tx,ty,tz)){73                 b[tx][ty][tz]=1;74                 if(a[tx][ty][tz]==‘.‘){75                     tmp.x=tx,tmp.y=ty,tmp.z=tz,tmp.step=ts;76                     pq.push(tmp);77                 }else{78                     return ts;79                 }80             }81         }82     }83     return -1;84 }

cdoj1088-王之迷宮 (三維迷宮最短路徑)【BFS】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.