社区讨论
求调
P1746离开中山路参与者 3已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mih8hvsa
- 此快照首次捕获于
- 2025/11/27 17:31 3 个月前
- 此快照最后确认于
- 2025/11/28 20:05 3 个月前
CPP
#include <bits/stdc++.h>
#define PII pair<int, int>
using namespace std;
int n;
const int N = 1005;
int sx, sy, fx, fy;
char g[N][N];
int st[N][N], dis[N][N];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void bfs (int x, int y) {
queue<PII> w;
w.push({x, y});
st[x][y] = 1;
while (w.size()) {
auto t = w.front();
w.pop();
if (t.first == fx && t.second == fy) {
cout << dis[t.first][t.second] << endl;
return;
}
for (int i = 0; i < 4; i++) {
int tx = t.first + dx[i];
int ty = t.second + dy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= n && g[tx][ty] == 0 && st[tx][ty] == 0) {
st[tx][ty] = 1;
dis[tx][ty] = dis[t.first][t.second] + 1;
w.push({tx, ty});
}
}
}
return;
}
int main () {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> g[i][j];
}
}
cin >> sx >> sy >> fx >> fy;
bfs (sx, sy);
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...