cstdio_luogu @ 2024-07-22 10:53:44
#include <bits/stdc++.h>
#define mian main
using namespace std;
int n,xx,yy,xxx,yyy;
int a[2005][2005];
int vis[2005][2005];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
struct Node {
int x,y,step;
};
void read() {
cin >> n;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
char c;
cin >> c;
a[i][j] = c - '0';
}
}
cin >> xx >> yy >> xxx >> yyy;
}
void bfs() {
queue<Node> q;
int x1 = xx,y1 = yy,x2 = xxx,y2 = yyy;
q.push({x1,y1,0});
int ans = 1e9;
while(!q.empty()) {
Node nw = q.front();
q.pop();
int x = nw.x,y = nw.y,step = nw.step;
vis[x][y] = 1;
if(x == x2 && y == y2) ans = min(ans,step);
for(int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 1 || nx > n || ny < 1 || ny > n || vis[nx][ny] == 1 || a[nx][ny] == 1) continue;
q.push({nx,ny,step+1});
}
}
cout << ans;
}
int mian() {
read();
bfs();
return 0;
}
by HAIN_ITDev @ 2024-07-22 10:56:41
死循环或者循环太多
by stem12345xi @ 2024-08-26 17:50:49
我#2过了,其他TLE......
by fmx169169169 @ 2024-08-31 23:42:50
把a[2005]和vis[2005]改成a[15]和vis[15],#2AC#3MLE,其它RE