T628045 迷宫问题
解题思路
其实就是 ➕ 中的记录路线的函数。这道题让我们将路径输出,那么更明显我们可以用递归输出,用 记录上一个位置,从而推出来,其次就是 了
参考代码
#include <iostream>
#include <queue>
using namespace std;
const int N = 10;
struct node
{
int x, y;
} pre[N][N];
int a[N][N], dx[4] = {0, 0, 1, -1}, dy[4] = {1, - 1, 0, 0};
bool f[N][N];
void bfs(int x, int y)
{
queue<node> q;
q.push({x, y});
while (!q.empty())
{
node t = q.front();
q.pop();
if (t.x == 4 && t.y == 4) return;
for (int i = 0; i < 4; i ++)
{
int nx = t.x + dx[i], ny = t.y + dy[i];
if (nx < 0 || nx >= N || ny < 0 || ny >= N || f[nx][ny] || a[nx][ny]) continue;
pre[nx][ny].x = t.x, pre[nx][ny].y = t.y;
f[nx][ny] = 1;
q.push({nx, ny});
}
}
return;
}
void print(int x, int y)
{
if (x == 0 && y == 0) {cout << "(0, 0)\n"; return;}
print(pre[x][y].x, pre[x][y].y);
cout << "(" << x << ", " << y << ")\n";
}
int main()
{
for (int i = 0; i < 5; i ++)
{
for (int j = 0; j < 5; j ++) cin >> a[i][j];
}
f[0][0] = 1;
bfs(0, 0);
print(4, 4);
return 0;
}T628044 迷宫之输出路径-困难版
解题思路
这道题更上一道题比较相似,只是在以前 数组更新的时候,顺便根据当前的循环,用个 类型的变量记录,所记录的就是上一个位置往哪走,走到的当前位置,不过切记如果还顺便用了个数组(题目要求输出的方向字母存储的地方),一定要与 、( 中, 用来使当前位置抵达下一个位置的数组)同步,还有,数组(题目要求输出的方向字母存储的地方)是固定的,因为题目说,如果两种方案,步数一样多,那么取字典序最小的一个,为了符合,所以一定是固定的
参考代码
#include <iostream>
#include <queue>
using namespace std;
const int N = 5005;
struct node
{
int x, y;
char ch;
}pre[N][N];
int n, m, dx[4] = {1, 0, 0, -1}, dy[4] = {0, -1, 1, 0};
char a[N][N], ch[4] = {'D', 'L', 'R', 'U'};
bool f[N][N];
void print(int x, int y)
{
if (x == 1 && y == 1) return;
print(pre[x][y].x, pre[x][y].y);
cout << pre[x][y].ch;
}
void bfs(int x, int y)
{
queue<node> q;
q.push({x, y});
while (!q.empty())
{
node t = q.front();
q.pop();
if (t.x == n && t.y == m) {print(t.x, t.y); return;}
for (int i = 0; i < 4; i ++)
{
int nx = t.x + dx[i], ny = t.y + dy[i];
if (nx < 1 || nx > n || ny < 1 || ny > m || f[nx][ny] || a[nx][ny] == '1') continue;
pre[nx][ny].x = t.x, pre[nx][ny].y = t.y;
pre[nx][ny].ch = ch[i];
f[nx][ny] = 1;
q.push({nx, ny});
}
}
cout << -1;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= m; j ++) cin >> a[i][j];
}
f[1][1] = 1;
bfs(1, 1);
return 0;
}P2360 地下城主
解题思路
这道题很明显是要开一个三维数组, 中的标记数组、队列、( 中, 用来使当前位置抵达下一个位置的数组)都要多一个维度,除了容易搞混以外,基本上没有难点。这里专门讲一下,因为题目中说可以去上层和下层,所以要开一个 , 怎么写呢,很明显,题目说只可以去上层和下层,所以里面只有 和 ,其余都是
参考代码
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int x, y, z;
};
const int N = 35;
int l, r, c, sx, sy, sz, tx, ty, tz, dy[] = {0, 0, 1, -1, 0, 0}, dz[] = {1, -1, 0, 0, 0, 0}, dx[] = {0, 0, 0, 0, 1, -1}, vis[N][N][N];
char a[N][N][N];
bool f;
void bfs(int x, int y, int z)
{
queue<node> q;
q.push({x, y, z});
while (!q.empty())
{
node t = q.front();
q.pop();
if (a[t.x][t.y][t.z] == 'E') {f = 1; return;}
for (int i = 0; i < 6; i ++)
{
int nx = t.x + dx[i], ny = t.y + dy[i], nz = t.z + dz[i];
if (nx < 1 || nx > l || ny < 1 || ny > r || nz < 1 || nz > c || vis[nx][ny][nz] || a[nx][ny][nz] == '#') continue;
vis[nx][ny][nz] = vis[t.x][t.y][t.z] + 1;
q.push({nx, ny, nz});
}
}
return;
}
int main()
{
cin >> l >> r >> c;
for (int i = 1; i <= l; i ++)
{
for (int j = 1; j <= r; j ++)
{
for (int k = 1; k <= c; k ++)
{
cin >> a[i][j][k];
if (a[i][j][k] == 'S') sx = i, sy = j, sz = k;
if (a[i][j][k] == 'E') tx = i, ty = j, tz = k;
}
}
}
vis[sx][sy][sz] = 1;
bfs(sx, sy, sz);
if (f) cout << "Escaped in " << vis[tx][ty][tz] - 1 << " minute(s).";
else cout << "Trapped!";
return 0;
}