Dungeon Master
2016-04-08 19:59
232 查看
题目链接:http://poj.org/problem?id=2251
本题有两个代码,区别在于输入的处理上面,而这道题在输入上面要特别注意,第一个代码需要在输入时加上“\n",而第二个代码就避免了这个问题,运用了一个temp,if 判断去限制读入的字符的种类。同时在输入过程中由于有三层地图,所以会有回车输入,第一个代码采用了getchar吃掉回车。其次在这个代码中犯了很多低级错误,在将队首元素入队弹出时,没有在while(!q.empty())里面进行,在输出时也要注意格式。还要注意本题三层地图,最里面一层的循环才是x的坐标,不要混淆。这道题由于有三层地图,因此需要开长度为6的数组。
第一个
本题有两个代码,区别在于输入的处理上面,而这道题在输入上面要特别注意,第一个代码需要在输入时加上“\n",而第二个代码就避免了这个问题,运用了一个temp,if 判断去限制读入的字符的种类。同时在输入过程中由于有三层地图,所以会有回车输入,第一个代码采用了getchar吃掉回车。其次在这个代码中犯了很多低级错误,在将队首元素入队弹出时,没有在while(!q.empty())里面进行,在输出时也要注意格式。还要注意本题三层地图,最里面一层的循环才是x的坐标,不要混淆。这道题由于有三层地图,因此需要开长度为6的数组。
第一个
#include<iostream> #include<queue> #include <cstdio> #include <cstring> using namespace std; const int dx[6]={1,-1,0,0,0,0}; const int dy[6]={0,0,1,-1,0,0}; const int dz[6]={0,0,0,0,1,-1}; const int maxn=35; struct node { int x,y,z; int s; }; node start,ending; node New,Now; queue<node>q; bool f[maxn][maxn][maxn]; char m[maxn][maxn][maxn]; int i, j, k; int L,R,C; int x,y,z; int bfs() { while(!q.empty()) { Now = q.front(); q.pop(); for( i=0;i<6;++i) { New.x=Now.x+dx[i]; New.y=Now.y+dy[i]; New.z=Now.z+dz[i]; New.s=Now.s+1; if(m[New.z][New.y][New.x]=='#'||f[New.z][New.y][New.x]) continue; q.push(New); f[New.z][New.y][New.x]=true; if(New.x==ending.x&&New.y==ending.y&&New.z==ending.z) return New.s; } } return -1; } int main() { while(scanf("%d%d%d\n",&L,&R,&C)&&R&&C&&L) { memset(f,0,sizeof(f)); memset(m,'#',sizeof(m)); while(!q.empty()) q.pop(); for( i = 1;i<= L;++i) { for(j = 1;j<= R;++j) { for( k = 1;k<= C;++k) { m[i][j][k] = getchar(); if(m[i][j][k]=='S') { f[i][j][k]=true; start.x = k; start.y = j; start.z = i; start.s = 0; q.push(start); } if(m[i][j][k]=='E') { ending.x = k; ending.y = j; ending.z = i; } } getchar(); } getchar(); } int ans=bfs(); if( ans <0) puts("Trapped!"); else cout << "Escaped in " <<ans << " minute(s)." << endl; } return 0; }*/ /*#include <cstdio> #include <cstring> #include <queue> #include<iostream> using namespace std; const int dx[6] = {1, -1, 0, 0, 0, 0}; const int dy[6] = {0, 0, 1, -1, 0, 0}; const int dz[6] = {0, 0, 0, 0, 1, -1}; const int maxn = 35; struct node { int x, y, z, step; } tmp; node S,T; node now; queue<node> q; int n, m, t, x, y, z, i, j, k; bool vis[maxn][maxn][maxn]; char mp[maxn][maxn][maxn]; int bfs() { while (!q.empty()) { now = q.front(); q.pop(); for (i = 0; i < 6; ++i) { tmp.x = now.x + dx[i]; tmp.y = now.y + dy[i]; tmp.z = now.z + dz[i]; tmp.step = now.step + 1; if(mp[tmp.z][tmp.y][tmp.x] == '#' ||vis[tmp.z][tmp.y][tmp.x] ) continue; q.push(tmp); vis[tmp.z][tmp.y][tmp.x] = true; if(tmp.x == T.x && tmp.y == T.y && tmp.z == T.z) return tmp.step; } } return -1; } int main() { while (scanf("%d%d%d\n", &t, &n, &m)&& t && m && n) { memset(vis, 0, sizeof(vis)); memset(mp, '#', sizeof(mp)); while (!q.empty()) q.pop(); for (i = 1; i <= t; ++i) { for (j = 1; j <= n; ++j) { for (k = 1; k <= m; ++k) { mp[i][j][k] = getchar(); if (mp[i][j][k] == 'S') { vis[i][j][k] = true; S.x = k; S.y = j; S.z = i; S.step = 0; q.push(S); } if (mp[i][j][k] == 'E') { T.x = k; T.y = j; T.z = i; } } getchar(); } getchar(); } int ans = bfs(); if (ans<0) puts("Trapped!"); else cout << "Escaped in " <<ans << " minute(s)." << endl; } return 0; }第二个
#include<iostream> using namespace std; #include<queue> #include<cstring> #include<string> char temp; bool vis[50][50][50]; bool vist[50][50][50]; const int dx[6] = {1,-1,0,0,0,0}; const int dy[6] = {0,0,1,-1,0,0}; const int dz[6] = {0,0,0,0,-1,1}; struct node { int x,y,z; int step; }; node now; node S,T; int bfs() { memset(vis,false,sizeof(vis)); queue<node>q; q.push(S); while(!q.empty()) { now = q.front(); q.pop(); for(int i=0; i<6; i++) { node New; New.x = now.x + dx[i]; New.y = now.y + dy[i]; New.z = now.z + dz[i]; New.step = now.step + 1; if(!vist[New.x][New.y][New.z] || vis[New.x][New.y][New.z]) continue; q.push(New); vis[New.x][New.y][New.z] = true; if(New.x == T.x && New.y == T.y && New.z == T.z) return New.step; } } return -1; } int main() { int L, R, C; while(cin >> L >> R >> C && L) { memset(vist,false,sizeof(vist)); for(int i=1; i<=L; i++) { for(int j=1; j<=R; j++) { for(int k=1; k<=C; k++) { cin >> temp; if(temp == 'S') { vist[i][j][k] = true; S.x = i; S.y = j; S.z = k; S.step = 0; } if(temp == 'E') { vist[i][j][k] = true; T.x = i; T.y = j; T.z = k; } if(temp == '.') { vist[i][j][k] = true; } } } } int ans = bfs(); if (ans<0) cout <<"Trapped!" << endl; else cout << "Escaped in " <<ans << " minute(s)." << endl; } return 0; }
相关文章推荐
- SAX解析与DOM解析对比
- [学习OpenCV攻略][016][RedHat下安装OpenCV]
- 线程通信
- poj 3159 Candies
- marquee
- POJ 2429 GCD & LCM Inverse
- bzoj2141 树状数组套Treap树
- android 图片文字轮播效果(图片和文字自动滚动)
- HDU 5446 Unknown Treasure Lucas+CRT
- DNS 理论
- 浅谈inode
- javascript中Uint8Array类型的subarray方法陷阱
- c++学习笔记(一)--如何在mac 下编译c文件
- poj2761(Treap)
- 求阶乘
- poj 2152 Fire 树形dp
- Android简单获取手机联系人姓名电话号码
- leetcode:Contains Duplicate
- 纸片:二分法
- Thrift,protobuf,json,xml,binary之间的对比