各路大神,救救蒟蒻吧
by cout_jerry @ 2022-08-20 22:10:53
@[cout_jerry](/user/755114) 边界
by NightTide @ 2022-08-20 22:28:06
看这个:
```cpp
#include<bits/stdc++.h>
#define MAXN 1010
using namespace std;
const int rx[10] = {0, 1, -1, 0, 0};
const int ry[10] = {0, 0, 0, 1, -1};
struct node{
int x, y, dis;
};
int n, xs, xt, ys, yt;
int mapp[MAXN][MAXN];
bool vis[MAXN][MAXN];
void bfs(node st, node ed){
queue<node> q; q.push(st);
vis[st.x][st.y] = true;
while(!q.empty()){
node now = q.front(); q.pop();
for(int i = 1; i <= 4; i++){
node to = (node){now.x + rx[i], now.y + ry[i], now.dis + 1};
if(to.x < 1 || to.y < 1 || to.x > n || to.y > n) continue;//这里记得判断边界
if(vis[to.x][to.y] || mapp[to.x][to.y]) continue;
vis[to.x][to.y] = true;
q.push(to);
if(to.x == ed.x && to.y == ed.y){
printf("%d\n",to.dis);
return ;
}
}
}
}
int main(){
scanf("%d",&n);
for(int i = 1; i <= n; i++){
char s[MAXN];
scanf("%s",s + 1);
for(int j = 1; j <= n; j++){
mapp[i][j] = s[j] - '0';
}
}
scanf("%d%d%d%d",&xs,&ys,&xt,&yt);
bfs((node){xs, ys, 0}, (node){xt, yt, 0});
return 0;
}
```
by NightTide @ 2022-08-20 22:28:56
@[Hoshino_kaede](/user/547908) ???麻烦说详细点
by cout_jerry @ 2022-08-20 22:28:58
@[Hoshino_kaede](/user/547908) 边界不知道哪错了
by cout_jerry @ 2022-08-20 22:29:31
@[cout_jerry](/user/755114) 你代码不是枚举上下左右移动吗?移动的时候比如 `x - 1` 是有可能将 `x` 减成负数的,`x + 1` 也可能超过 $n$,`y` 同理
by NightTide @ 2022-08-20 22:30:53
@[cout_jerry](/user/755114) 你这个bfs假了啊,如果这个点已经被搜过那么就不要再搜,你并没有体现这个事情
by bamboo1030 @ 2022-08-20 22:31:18
对,这也是一方面,加一个 $vis$ 数组
by NightTide @ 2022-08-20 22:31:53
@[cout_jerry](/user/755114)
by NightTide @ 2022-08-20 22:32:05
@[bamboo123](/user/369181) 感谢这位大佬的指点
by cout_jerry @ 2022-08-20 22:33:06