Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贡献n次WA,一定记得把队列给清空!!!!贴代码~~~#include <iostream> #include <queue> using namespace std; char ddd[31][31][31]; int flag; int l,r,c; struct xyz { int x; int y; int z; }s,e; struct cd { int vis; int num; int x; int y; int z; }cddd[31][31][31]; void init() { for(int i = 0;i < 31;i++) for(int j = 0;j < 31;j++) for(int k = 0;k < 31;k++) { cddd[i][j][k].num = 0; cddd[i][j][k].vis = 0; cddd[i][j][k].x = i; cddd[i][j][k].y = j; cddd[i][j][k].z = k; } flag = 0; } int main() { int i,j,k; queue <cd> q; while(scanf("%d%d%d",&l,&r,&c) != EOF && l != 0&&r != 0&&c != 0) { init(); for(i = 0;i < l;i++) for(j = 0;j < r;j++) { scanf("%s",ddd[i][j]); for(k = 0;k < c;k++) { if(ddd[i][j][k] == 'S') { s.x = i; s.y = j; s.z = k; } if(ddd[i][j][k] == 'E') { e.x = i; e.y = j; e.z = k; } } } q.push(cddd[s.x][s.y][s.z]); cddd[s.x][s.y][s.z].vis = 1; while(!q.empty()) { cd front = q.front(); int cx = front.x; int cy = front.y; int cz = front.z; if(cddd[e.x][e.y][e.z].vis) { flag = 1; break; } q.pop(); if(!cddd[cx + 1][cy][cz].vis && cx + 1 < l && ddd[cx + 1][cy][cz] == '.' || ddd[cx + 1][cy][cz] == 'E') { q.push(cddd[cx + 1][cy][cz]); cddd[cx + 1][cy][cz].num += cddd[cx][cy][cz].num + 1; cddd[cx + 1][cy][cz].vis = 1; } if(!cddd[cx - 1][cy][cz].vis && cx - 1 >= 0 && ddd[cx - 1][cy][cz] == '.' || ddd[cx - 1][cy][cz] == 'E') { q.push(cddd[cx - 1][cy][cz]); cddd[cx - 1][cy][cz].num += cddd[cx][cy][cz].num + 1; cddd[cx - 1][cy][cz].vis = 1; } if(!cddd[cx][cy + 1][cz].vis && cy + 1 < r && ddd[cx][cy + 1][cz] == '.' || ddd[cx][cy + 1][cz] == 'E') { q.push(cddd[cx][cy + 1][cz]); cddd[cx][cy + 1][cz].num += cddd[cx][cy][cz].num + 1; cddd[cx][cy + 1][cz].vis = 1; } if(!cddd[cx][cy - 1][cz].vis && cy - 1 >= 0 && ddd[cx][cy - 1][cz] == '.' || ddd[cx][cy - 1][cz] == 'E') { q.push(cddd[cx][cy - 1][cz]); cddd[cx][cy - 1][cz].num += cddd[cx][cy][cz].num + 1; cddd[cx][cy - 1][cz].vis = 1; } if(!cddd[cx][cy][cz + 1].vis && cz + 1 < c && ddd[cx][cy][cz + 1] == '.' || ddd[cx][cy][cz + 1] == 'E') { q.push(cddd[cx][cy][cz + 1]); cddd[cx][cy][cz + 1].num += cddd[cx][cy][cz].num + 1; cddd[cx][cy][cz + 1].vis = 1; } if(!cddd[cx][cy][cz - 1].vis && cz - 1 >= 0 && ddd[cx][cy][cz - 1] == '.' || ddd[cx][cy][cz - 1] == 'E') { q.push(cddd[cx][cy][cz - 1]); cddd[cx][cy][cz - 1].num += cddd[cx][cy][cz].num + 1; cddd[cx][cy][cz - 1].vis = 1; } } while(!q.empty()) q.pop(); if(flag) printf("Escaped in %d minute(s).\n",cddd[e.x][e.y][e.z].num); else printf("Trapped!\n"); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator