题目:B - Dungeon Master
题目描述:
思路:普通bfs模板变为三维图
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
/*三维bfs*/
const int N = 101;
int flag = 0;
int g[N][N][N];
int res[N][N][N];
int l, r, c;
int dx[6] = {1, 0, -1, 0, 0, 0}, dy[6] = {0, 1, 0, -1, 0, 0}, dz[6] = {0, 0, 0, 0, -1, 1};
struct no
{
int z, x, y;
}be, ed;
queue<no> qu;
void bfs()
{
res[be.z][be.x][be.y] = 0;
qu.push(be);
while(!qu.empty())
{
no t;
t = qu.front();
qu.pop();
for(int i = 0; i < 6; i ++)
{
int tz = t.z + dz[i];
int tx = t.x + dx[i];
int ty = t.y + dy[i];
if(tz < 1 || tx < 1 || ty < 1 || tz > l || tx > r || ty > c || res[tz][tx][ty] != -1 || g[tz][tx][ty] == '#') continue;
qu.push({tz, tx, ty});
// st[tz][tx][ty] = 1;
res[tz][tx][ty] = res[t.z][t.x][t.y] + 1;
}
}
}
int main()
{
while(cin >> l >> r >> c)
{
if(l == 0 && r == 0 && c == 0) break;
memset(res, -1, sizeof res);
for(int i = 0; i < l; i ++)
{
for(int j = 0; j < r; j ++)
{
string mp;
cin >> mp;
for(int k = 0; k < c; k ++)
{
g[i + 1][j + 1][k + 1] = mp[k];
if(mp[k] == 'S') be = {i + 1, j + 1, k + 1};
else if(mp[k] == 'E') ed = {i + 1, j + 1, k + 1};
}
}
}
bfs();
int ans = res[ed.z][ed.x][ed.y];
if(ans != -1) printf("Escaped in %d minute(s).\n",ans);
else cout << "Trapped!" << endl;
}
return 0;
}