解题思路:本题就是一个简单的三维bfs题,使用PIII或者结构体储存好坐标点,然后定义好偏移量,接下来就可以直接按照bfs的套路写即可
C++代码
#include <bits/stdc++.h>
// #define int long long
#define x first
#define y second
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
char g[N][N][N];
int dist[N][N][N];
int l, r, c;
PIII ss, ee;
int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};
void bfs()
{
memset(dist, 0x3f, sizeof dist);
queue<PIII> q;
q.push(ss);
dist[ss.x.x][ss.x.y][ss.y] = 0;
while (q.size())
{
auto t = q.front();
q.pop();
int xx = t.x.x, yy = t.x.y, zz = t.y;
for (int i = 0; i < 6; i ++ )
{
int aa = xx + dx[i], bb = yy + dy[i], cc = zz + dz[i];
if (aa >= 0 && aa < l && bb >= 0 && bb < r && cc >= 0 && cc < c && dist[aa][bb][cc] == 0x3f3f3f3f && g[aa][bb][cc] != '#')
{
dist[aa][bb][cc] = dist[xx][yy][zz] + 1;
q.push({{aa, bb}, cc});
}
}
}
}
signed main()
{
while (cin >> l >> r >> c, l || r || c)
{
for (int i = 0; i < l; i ++ )
{
for (int j = 0; j < r; j ++ )
{
for (int k = 0; k < c; k ++ )
{
cin >> g[i][j][k];
if (g[i][j][k] == 'S') ss = {{i, j}, k};
if (g[i][j][k] == 'E') ee = {{i, j}, k};
}
}
}
bfs();
if (dist[ee.x.x][ee.x.y][ee.y] != 0x3f3f3f3f) cout << "Escaped in " << dist[ee.x.x][ee.x.y][ee.y] << " minute(s)." <<endl;
else cout << "Trapped!" << endl;
}
return 0;
}
创作不易,大家可以在AcWing注册账号,参与一下AC之星计划,我的邀请码是MYRGS。对题解有任何疑问可以在评论区下面评论