int dx[] = {1,0,0,-1,0,0};
int dy[] = {0,1,0,0,-1,0};
int dz[] = {0,0,1,0,0,-1};
char mp[35][35][35];
bool vis[35][35][35];
int sx, sy, sz, ex, ey, ez,ans;
int l, r, c;
struct node {
int x, y, z, len;
};
bool check(node e)
{
if (e.x<1 || e.y<1 || e.z<1 || e.x>l || e.y>r || e.z>c)return false;
if (mp[e.x][e.y][e.z] == '#')return false;
if (vis[e.x][e.y][e.z])return false;
return true;
}
void bfs()
{
queue<node> q;
q.push(node{ sx,sy,sz,0 });vis[sx][sy][sz] = true;
while (!q.empty())
{
node now = q.front();q.pop();
f(i, 0, 5)
{
node nextnode = node{ now.x + dx[i],now.y + dy[i],now.z + dz[i],now.len+1 };
if (check(nextnode))
{
q.push(nextnode);
if (nextnode.x == ex && nextnode.y == ey && nextnode.z == ez) { ans = nextnode.len;return; }
//cout << nextnode.x << " " << nextnode.y << " " << nextnode.z << endl;
vis[nextnode.x][nextnode.y][nextnode.z] = true;
}
}
}
return;
}
int main()
{
while (cin >> l >> r >> c)
{
if (l == 0 && r == 0 && c == 0)break;
memset(vis, false, sizeof vis);
f(i, 1, l)
{
f(j, 1, r)
{
scanf("%s", mp[i][j] + 1);
f(k, 1, c)if (mp[i][j][k] == 'S') { sx = i, sy = j, sz = k;break; }
f(k, 1, c)if (mp[i][j][k] == 'E') { ex = i, ey = j, ez = k;break; }
}
}
ans = 0;bfs();
if (ans == 0) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n", ans);
}
return 0;
}
Dungeon Master POJ - 2251(三维bfs)
最新推荐文章于 2022-10-14 19:40:10 发布