Commandant @ 2021-10-22 19:09:38
Code:
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1010;
int n;
string maze[maxn];
int sx, sy, ex, ey;
bool vis[maxn][maxn];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ans[maxn][maxn];
#define mp make_pair
bool in(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n;
}
int bfs() {
queue<pair<int, int> > q;
q.push(mp(sx, sy));
vis[sx][sy] = true;
while (!q.empty()) {
pair<int, int> now = q.front();
q.pop();
if (now.first == ex && now.second == ey) {
return ans[ex][ey];
}
for (int i = 0;i < 4;i++) {
int tx = now.first + dx[i];
int ty = now.second + dy[i];
if (in(tx, ty) && !vis[tx][ty] && maze[tx][ty] == '0') {
q.push(mp(tx, ty));
vis[tx][ty] = true;
ans[tx][ty] = ans[now.first][now.second] + 1;
}
}
}
return -1;
}
int main() {
cin >> n;
for (int i = 1;i <= n;i++) {
cin >> maze[i];
}
cin >> sx >> sy >> ex >> ey;
cout << bfs() << endl;
return 0;
}
by Ninelife_Cat @ 2021-10-22 19:13:25
@46tni
for (int i = 1;i <= n;i++) {
cin >> maze[i];
maze[i] = ' ' + maze[i]
}
string
的下标是从
by Commandant @ 2021-10-22 19:16:34
@Ninelife_Cat 感谢