题目链接: http://poj.org/problem?id=2251
题意:现有一个三维的地牢,问你能否逃出。
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> #include<queue> #include<iostream> using namespace std; #define maxn 50 #define INF 0xfffffff int dir[6][3] = { {1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0} }; int l, r, c; char maps[maxn][maxn][maxn]; int v[maxn][maxn][maxn]; struct node { int x, y, z; int step; }; int BFS(node s, node e) { queue<node>Q; s.step = 0; Q.push(s); while(Q.size()) { node now, next; now = Q.front(); Q.pop(); if(now.x == e.x && now.y == e.y && now.z == e.z) return now.step; for(int i=0; i<6; i++) { next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; next.z = now.z + dir[i][2]; if(next.x>=0 && next.x<l && next.y>=0 && next.y<r && next.z>=0 && next.z<c && !v[next.x][next.y][next.z] && maps[next.x][next.y][next.z]!='#') { v[next.x][next.y][next.z] = 1; next.step = now.step + 1; Q.push(next); } } } return -1; } int main() { node s, e; while(scanf("%d %d %d", &l, &r, &c), l+r+c) { memset(v, 0, sizeof(v)); for(int i=0; i<l; i++) { for(int j=0; j<r; j++) { scanf("%s", maps[i][j]); for(int k=0; k<c; k++) { if(maps[i][j][k] == 'S') s.x=i,s.y=j,s.z=k; if(maps[i][j][k] == 'E') e.x=i,e.y=j,e.z=k; } } } int ans = BFS(s,e); if(ans == -1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } return 0; }