标签:ons south with bin ber int gen limited level
Input
Output
Escaped in x minute(s).where x is replaced by the shortest time it takes to escape. If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
三维bfs水题,但鉴于自己好久没有做bfs,忘记了vis数组放哪里和取值,最最重要的是中间一行代码写错了,害我debug半天
bfs模板题
#include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; int dx[]={1,0,-1,0,0,0},dy[]={0,-1,0,1,0,0},dz[]={0,0,0,0,1,-1}; int t,n,m,sx,sy,sz,ex,ey,ez,vis[50][50][50]; char mapn[50][50][50]; struct node{ int x,y,z,step; }; void bfs(){ node p; p.x = sx,p.y = sy,p.z = sz; vis[sx][sy][sz] = 1; p.step = 0; queue<node> q; q.push(p); while(!q.empty()){ node tmp = q.front(); q.pop(); if(tmp.x == ex && tmp.y == ey && tmp.z == ez){ cout << "Escaped in " << tmp.step << " minute(s)." << endl; return; } for(int i=0;i<6;i++){ int xx = tmp.x + dx[i]; int yy = tmp.y + dy[i]; int zz = tmp.z + dz[i]; if(mapn[xx][yy][zz] != ‘#‘ && xx>0 && xx<=t && yy>0 && yy<=n && zz>0 && zz<=m && !vis[xx][yy][zz]){ node tp; tp.x = xx; tp.y = yy; tp.z = zz; tp.step = tmp.step + 1;//这一行代码,开始写成了tp.step++; vis[xx][yy][zz] = 1; q.push(tp); } } } cout << "Trapped!" << endl; } int main(){ while(cin >> t >> n >> m){ if(t == 0 || n == 0 || m == 0){ break; } memset(vis,0,sizeof(vis)); for(int i=1;i<=t;i++){ for(int j=1;j<=n;j++){ for(int k=1;k<=m;k++){ cin >> mapn[i][j][k]; if(mapn[i][j][k] == ‘S‘){ sx = i,sy = j,sz = k; } if(mapn[i][j][k] == ‘E‘){ ex = i,ey = j,ez = k; } } } } bfs(); } return 0; }
Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
标签:ons south with bin ber int gen limited level
原文地址:http://www.cnblogs.com/l609929321/p/7513873.html