题目点这里 附我的代码(为什么输出会错)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
struct node{
int x, y;
};
int g[5][5]; //地图
int dist[5][5]; //从起点走到(i, j)这个点最少走几步(distance)
bool vis[5][5 ]; //是否被遍历过(visited)
int dx[4] = {0, 0, 1, -1}; //水平方向偏移量数组
int dy[4] = {-1, 1, 0, 0}; //垂直方向偏移量数组
void bfs(int sx, int sy) { //sx:startx sy:starty
queue<node> q;
q.push({sx, sy});
dist[sx][sy] = 1;
vis[sx][sy] = true;
while (!q.empty()) {
//1.从队首取一个元素f出来
node f = q.front();
q.pop();
//2.在队尾把f的所有邻居考虑加进去
for (int i = 0; i < 4; i++) {
int nx = f.x + dx[i];
int ny = f.y + dy[i];
if (nx >= 0 && ny >= 0 && nx < 5 && ny < 5 && g[nx][ny] == 0 && !vis[nx][ny]) {
cout << "(" << nx << ',' << ny << ")"<< endl;
q.push({nx, ny});
dist[nx][ny] = 1 + dist[f.x][f.y];
vis[nx][ny] = true;
}
}
}
}
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cin >> g[i][j];
}
}
bfs(0, 0);
return 0;
}