Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18949 | Accepted: 7344 |
Description
Input
Output
Escaped in x minute(s).
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基础题:
#include
#include
#include
#include
#include
using namespace std;
int vis[31][31][31];
char map[31][31][31];
int sx, sy, sz, ex, ey, ez;
int L, R, C;
struct node
{
int x, y, z, step;
friend bool operator < (node a, node b)
{
return a.step > b.step;
}
};
void getmap()
{
int i, j, k;
//getchar();
for(i = 0; i < L; i++)
{
for(j = 0; j < R; j++)
{
getchar();
for(k = 0; k < C; k++)
{
scanf("%c", &map[i][j][k]);
if(map[i][j][k] == 'S')
{
sx = i;
sy = j;
sz = k;
}
if(map[i][j][k] == 'E')
{
ex = i;
ey = j;
ez = k;
}
}
//getchar();
}
getchar();
}
}
int judge(int x, int y, int z)
{
if(map[x][y][z] != '#' && !vis[x][y][z] && x >= 0 && x < L && y >= 0 && y < R && z >= 0 && z < C)
return 1;
else
return 0;
}
void bfs()
{
node now, next;
priority_queue q;
int i, j, k;
int move[6][3] = {1,0,0, -1,0,0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1};
memset(vis, 0, sizeof(vis));
now.x = sx;
now.y = sy;
now.z = sz;
now.step = 0;
q.push(now);
vis[sx][sy][sz] = 1;
while(!q.empty())
{
now = q.top();
q.pop();
if(now.x == ex && now.y == ey && now.z == ez)
{
printf("Escaped in %d minute(s).\n", now.step);
return ;
}
for(k = 0; k < 6; k++)
{
next.x = now.x + move[k][0];
next.y = now.y + move[k][1];
next.z = now.z + move[k][2];
if(judge(next.x, next.y, next.z))
{
next.step = now.step + 1;
vis[next.x][next.y][next.z] = 1;
q.push(next);
}
}
}
printf("Trapped!\n");
}
int main()
{
while(scanf("%d%d%d", &L, &R, &C),L||R||C)
{
getmap();
bfs();
}
return 0;
}