poj 2251 Dungeon Master (三维bfs)
生活随笔
收集整理的這篇文章主要介紹了
poj 2251 Dungeon Master (三维bfs)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://poj.org/problem?id=2251
簡單bfs,只不過是三維的。。。
唯一的坑點在輸出上...
Escaped in %d minute(s)這意思是答案為1輸出minute,不為1輸出minutes還是說是不是1都輸出minute(s)?
試了下,答案是后者。
?
另:終于找到了好的讀地圖的方法。。。而不用擔心回車符。
就是先讀成字符串。
具體見代碼
?
/*************************************************************************> File Name: code/2015summer/searching/B.cpp> Author: 111qqz> Email: rkz2013@126.com > Created Time: 2015年07月17日 星期五 16時47分46秒************************************************************************/#include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> using namespace std; #define REP(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef unsigned long long ULL; const int N=40; char st[N][N][N]; int d[N][N][N]; int l,r,c; int sx,sy,sz,tx,ty,tz; int dirx[6]={1,-1,0,0,0,0}; int diry[6]={0,0,-1,1,0,0}; int dirz[6]={0,0,0,0,1,-1};bool ok(int x,int y,int z) {if (z>=0&&z<l&&x>=0&&x<r&&y>=0&&y<c&&d[z][x][y]==-1&&st[z][x][y]!='#')return true;return false; }void bfs() {memset(d,-1,sizeof(d));queue<int>x;queue<int>y;queue<int>z;x.push(sx);y.push(sy);z.push(sz);d[sz][sx][sy]=0;while (!x.empty()&&!y.empty()&&!z.empty()){int prex = x.front();x.pop();int prey = y.front();y.pop();int prez = z.front();z.pop();for ( int i = 0 ; i < 6 ; i++ ){int newx = prex+dirx[i];int newy = prey+diry[i];int newz = prez+dirz[i];bool flag = ok(newx,newy,newz);if (flag){d[newz][newx][newy]=d[prez][prex][prey]+1;x.push(newx);y.push(newy);z.push(newz);}}} }int main() {while (scanf("%d %d %d",&l,&r,&c)){if (l==0&&r==0&&c==0)break;for ( int i = 0 ; i < l ; i++){for ( int j = 0 ; j < r; j++)scanf("%s",st[i][j]);}for ( int i = 0 ; i < l ; i++ ){for ( int j = 0 ; j < r ; j ++){for ( int k = 0 ; k < c ; k++ ){if (st[i][j][k]=='S'){sx = j;sy = k;sz = i;}if (st[i][j][k]=='E'){tx = j;ty = k;tz = i;}}}}bfs();int ans = d[tz][tx][ty];if (ans==-1){printf("Trapped!\n");}else{// if (ans==1)// printf("Escaped in 1 minute.\n");// else printf("Escaped in %d minutes.\n",ans);printf("Escaped in %d minute(s).\n",ans);}} }?
轉載于:https://www.cnblogs.com/111qqz/p/4664170.html
總結
以上是生活随笔為你收集整理的poj 2251 Dungeon Master (三维bfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 控制台浏览器代码实战
- 下一篇: 软件项目如何选型