T628045 迷宫问题
用 数组记录原位置,用BFS搜索到达更近的位置。到终点时输出答案即可。
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[100 + 9][100 + 9];
bool vis[100 + 9][100 + 9];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int ha, la, hb, lb;//起点坐标,终点坐标
struct node
{
int x, y, step;
}pre[100 + 9][100 + 9];
void print(int x, int y)
{
if (x == 1 && y == 1)//当前要输出起点
{
cout << "(0, 0)\n";//输出起点(0, 0)
return;
}
print(pre[x][y].x, pre[x][y].y);
cout << "(" << x - 1 << ", " << y - 1 << ")\n";//因为从0开始,所以要-1
}
void BFS(int sx, int sy)
{
queue <node> q;
q.push({sx, sy, 0});//压入起点(sx, sy)
while (q.empty() == false)//队列不为空
{
node cur = q.front();
q.pop();
if (cur.x == 5 && cur.y == 5)//到了终点(5, 5)
{
print(5, 5);//输出答案
return;
}
for (int i = 0; i <= 3; ++ i)
{
int nx = dx[i] + cur.x;//向各方向走1步
int ny = dy[i] + cur.y;//向各方向走1步
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false && a[nx][ny] != '1')//可以走
{
node nex = {nx, ny, cur.step + 1};
pre[nx][ny] = {cur.x, cur.y};//记录答案
vis[nx][ny] = true;//(nx, ny)走过了
q.push(nex);//压入新起点
}
}
}
}
int main()
{
n = 5;//五行
m = 5;//五列
for (int i = 1; i <= n; ++ i)
{
for (int j = 1; j <= m; ++ j)
{
cin >> a[i][j];
}
}
BFS(1, 1);
return 0;
}T628044 迷宫之输出路径-困难版
同T628045 迷宫问题,只需要多记录一个走过来的方向,最后再输出方向 即可。
#include <bits/stdc++.h>
using namespace std;
int n, m;
char k[] = {'D', 'L', 'R', 'U'};//优先级,D>L>R>U
char a[5000 + 9][5000 + 9];
bool vis[5000 + 9][5000 + 9];
int dx[4] = {1, 0, 0, -1};//方向优先级
int dy[4] = {0, -1, 1, 0};//方向优先级
int ha, la, hb, lb;
struct node//正常记录的node
{
int x, y;
};
struct node2//记录方向的node
{
int x, y;
char step;
}pre[5000 + 9][5000 + 9];
void print(int x, int y)
{
if (x == 0 && y == 0)
{
return;
}
print(pre[x][y].x, pre[x][y].y);
cout << pre[x][y].step;//只输出方向step
}
void BFS(int sx, int sy)
{
vis[sx][sy] = true;
queue <node> q;
q.push({sx, sy});//压入起点
while (q.empty() == false)
{
node cur = q.front();
q.pop();
if (cur.x == n - 1 && cur.y == m - 1)//到达终点
{
print(n - 1, m - 1);//输出方向
return;
}
for (int i = 0; i <= 3; ++ i)
{
int nx = dx[i] + cur.x;//走向下一个位置
int ny = dy[i] + cur.y;//走向下一个位置
if (nx >= 0 && nx < n && ny >= 0 && ny < m && vis[nx][ny] == false && a[nx][ny] != '1')//可以走
{
node nex = {nx, ny};
pre[nx][ny] = {cur.x, cur.y, k[i]};//额外记录走过来的方向
vis[nx][ny] = true;//标记走过了
q.push(nex);//压入新起点
}
}
}
cout << -1;//没有通向终点的路
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; ++ i)
{
for (int j = 0; j < m; ++ j)
{
cin >> a[i][j];
}
}
BFS(0, 0);
return 0;
}P2360 地下城主
直接给二位的数组多加一维,变成三维。再把所有[...][...]类型的地方全部改成三维的即可。
#include <bits/stdc++.h>
using namespace std;
int n, m, o;
char a[100 + 9][100 + 9][100 + 9];
bool vis[100 + 9][100 + 9][100 + 9];
int dx[] = {0, 0, 1, -1, 0, 0};//可以走6个方向
int dy[] = {0, 0, 0, 0, 1, -1};
int dz[] = {1, -1, 0, 0, 0, 0};
int ha, la, hb, lb, ga, gb;
struct node
{
int x, y, z, step;//加上z坐标
};
int BFS(int sx, int sy, int sz)
{
queue <node> q;
q.push({sx, sy, sz, 0});
while (q.empty() == false)
{
node cur = q.front();
q.pop();
if (cur.x == hb && cur.y == lb && cur.z == gb)//到达终点
{
return cur.step;//返回步数
}
for (int i = 0; i <= 5; ++ i)
{
int nx = dx[i] + cur.x;//走到下一步x
int ny = dy[i] + cur.y;//走到下一步y
int nz = dz[i] + cur.z;//走到下一步z
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && nz >= 1 && nz <= o && vis[nx][ny][nz] == false && a[nx][ny][nz] != '#')//可以走到这一格子
{
node nex = {nx, ny, nz, cur.step + 1};
vis[nx][ny][nz] = true;//标记为走过了
q.push(nex);//压入新起点
}
}
}
return -1;//走不到终点
}
int main()
{
cin >> n >> m >> o;
for (int i = 1; i <= n; ++ i)
{
for (int j = 1; j <= m; ++ j)
{
for (int k = 1; k <= o; ++ k)
{
cin >> a[i][j][k];//读入三位数组
if (a[i][j][k] == 'S')
{
ha = i;
la = j;
ga = k;
}
if (a[i][j][k] == 'E')
{
hb = i;
lb = j;
gb = k;
}
}
}
}
int ans = BFS(ha, la, ga);
if (ans != -1)
{
cout << "Escaped in " << ans << " minute(s).";
}
else
{
cout << "Trapped!";
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[100 + 9][100 + 9];
bool vis[100 + 9][100 + 9];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int ha, la, hb, lb;//起点坐标,终点坐标
struct node
{
int x, y, step;
}pre[100 + 9][100 + 9];
void print(int x, int y)
{
if (x == 1 && y == 1)//当前要输出起点
{
cout << "(0, 0)\n";//输出起点(0, 0)
return;
}
print(pre[x][y].x, pre[x][y].y);
cout << "(" << x - 1 << ", " << y - 1 << ")\n";//因为从0开始,所以要-1
}
void BFS(int sx, int sy)
{
queue <node> q;
q.push({sx, sy, 0});//压入起点(sx, sy)
while (q.empty() == false)//队列不为空
{
node cur = q.front();
q.pop();
if (cur.x == 5 && cur.y == 5)//到了终点(5, 5)
{
print(5, 5);//输出答案
return;
}
for (int i = 0; i <= 3; ++ i)
{
int nx = dx[i] + cur.x;//向各方向走1步
int ny = dy[i] + cur.y;//向各方向走1步
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && vis[nx][ny] == false && a[nx][ny] != '1')//可以走
{
node nex = {nx, ny, cur.step + 1};
pre[nx][ny] = {cur.x, cur.y};//记录答案
vis[nx][ny] = true;//(nx, ny)走过了
q.push(nex);//压入新起点
}
}
}
}
int main()
{
n = 5;//五行
m = 5;//五列
for (int i = 1; i <= n; ++ i)
{
for (int j = 1; j <= m; ++ j)
{
cin >> a[i][j];
}
}
BFS(1, 1);
return 0;
}