本题题解由AI辅助生成,并经过一定改写。
使用广度优先搜索(BFS)解决。
定义部分
全局变量
int n, m, sx, sy, ex, ey;
vector<vector<char>> grid;
vector<vector<bool>> visited;
vector<vector<pair<int, int>>> parent;n 和 m:表示网格的行数和列数。
sx, sy:起点的行和列(输入的是 1 基索引,程序内部会转为 0 基)。
ex, ey:终点的行和列(输入的 1 基索引,转为 0 基索引)。
grid:存储网格地图信息,使用二维 vector<char>,每个位置可以是 =(可以通行)或 #(墙壁,无法通行)。
visited:一个二维的布尔型数组,用于标记某个位置是否被访问过,防止重复访问。
parent:记录每个位置的前驱节点,用于路径回溯。每个元素是一个 pair<int, int>,表示该位置的前一个坐标。
方向数组
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};这两个数组表示八个方向的移动。dx 和 dy 分别代表在 x 和 y 坐标上的偏移量。
有效性检查
bool isValid(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m) {
return false;
}
return grid[x][y] == '=' && !visited[x][y];
}该函数用于判断坐标 (x, y) 是否有效。
分别检查该格是否超出地图范围、是否可以通行、是否已被访问到。
路径输出
void printPath() {
vector<pair<int, int>> path;
pair<int, int> cur = make_pair(ex, ey);
while (cur.first != sx || cur.second != sy) {
path.push_back(cur);
cur = parent[cur.first][cur.second];
}
path.push_back(make_pair(sx, sy));
for (int i = path.size() - 1; i > 0; --i) {
cout << path[i].first + 1 << "," << path[i].second + 1 << " to "
<< path[i-1].first + 1 << "," << path[i-1].second + 1 << endl;
}
cout << "over" << endl;
}path:存储路径点的容器,起点和终点坐标会按从终点到起点的顺序存入其中。
cur = make_pair(ex, ey):从终点开始,逐步根据 parent 数组回溯路径。
while (cur.first != sx || cur.second != sy):路径回溯到起点时停止。
cur = parent[cur.first][cur.second:通过 parent 数组找到当前节点的前驱节点,并更新 cur。
path.push_back(make_pair(sx, sy)):最后将起点放入路径中。
for (int i = path.size() - 1; i > 0; --i):从路径的反向输出,每行输出从一个节点到下一个节点的坐标。
cout << "over":输出结束,表示路径找到。
BFS实现
void bfs() {
queue<pair<int, int>> q;
q.push(make_pair(sx, sy));
visited[sx][sy] = true;
while (!q.empty()) {
pair<int, int> current = q.front();
int x = current.first;
int y = current.second;
q.pop();
if (x == ex && y == ey) {
printPath();
return;
}
for (int i = 0; i < 8; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (isValid(nx, ny)) {
visited[nx][ny] = true;
parent[nx][ny] = make_pair(x, y);
q.push(make_pair(nx, ny));
}
}
}
cout << "can't" << endl;
}queue<pair<int, int>> q:队列用于存储当前要访问的节点。
q.push(make_pair(sx, sy)):将起点加入队列,并标记为已访问。
while (!q.empty()):不断从队列中取出节点进行扩展,直到队列为空。
pair<int, int> current = q.front():获取当前节点的坐标。
q.pop():将该节点从队列中移除。
if (x == ex && y == ey):如果当前节点是终点,调用 printPath() 输出路径,结束程序。
for (int i = 0; i < 8; ++i):循环遍历八个方向。
if (isValid(nx, ny)):如果新位置有效,标记为已访问,并将该位置的前驱节点记录在 parent 中,然后将新位置加入队列。
如果搜索结束后没有找到终点,输出 can't 表示无解。
主函数
int main() {
cin >> n >> m;
cin >> sx >> sy;
cin >> ex >> ey;
--sx; --sy; --ex; --ey;
grid.resize(n, vector<char>(m));
visited.resize(n, vector<bool>(m, false));
parent.resize(n, vector<pair<int, int>>(m, make_pair(-1, -1)));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> grid[i][j];
}
}
if (grid[sx][sy] == '#' || grid[ex][ey] == '#') {
cout << "can't" << endl;
} else {
bfs();
}
return 0;
}-
输入
-
初始化
-
读取网格布局
-
判断是否为
can't的特殊情况 -
调用DFS
时间复杂度
15ms,正解。
复杂度 ,因为保证每个格子只访问一次。
完整代码
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n, m, sx, sy, ex, ey;
vector<vector<char>> grid;
vector<vector<bool>> visited;
vector<vector<pair<int, int>>> parent;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; // 八个方向的x偏移量
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; // 八个方向的y偏移量
bool isValid(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= m) {
return false;
}
return grid[x][y] == '=' && !visited[x][y];
}
void printPath() {
vector<pair<int, int>> path;
pair<int, int> cur = make_pair(ex, ey);
while (cur.first != sx || cur.second != sy) {
path.push_back(cur);
cur = parent[cur.first][cur.second];
}
path.push_back(make_pair(sx, sy));
for (int i = path.size() - 1; i > 0; --i) {
cout << path[i].first + 1 << "," << path[i].second + 1 << " to "
<< path[i-1].first + 1 << "," << path[i-1].second + 1 << endl;
}
cout << "over" ;
}
void bfs() {
queue<pair<int, int>> q;
q.push(make_pair(sx, sy));
visited[sx][sy] = true;
while (!q.empty()) {
pair<int, int> current = q.front();
int x = current.first;
int y = current.second;
q.pop();
if (x == ex && y == ey) {
printPath();
return;
}
for (int i = 0; i < 8; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (isValid(nx, ny)) {
visited[nx][ny] = true;
parent[nx][ny] = make_pair(x, y);
q.push(make_pair(nx, ny));
}
}
}
cout << "can't" ;
}
int main() {
cin >> n >> m;
cin >> sx >> sy;
cin >> ex >> ey;
--sx; --sy; --ex; --ey; // 转换为0索引
grid.resize(n, vector<char>(m));
visited.resize(n, vector<bool>(m, false));
parent.resize(n, vector<pair<int, int>>(m, make_pair(-1, -1)));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> grid[i][j];
}
}
if (grid[sx][sy] == '#' || grid[ex][ey] == '#') {
cout << "can't" ;
} else {
bfs();
}
return 0;
}