80分,求大佬指点

P1746 离开中山路

```cpp #include <bits/stdc++.h> using namespace std; int n,sx,sy,ex,ey,vis[1001][1001]; char a[1001][1001]; int dx[]={-1,1,0,0}; int dy[]={0,0,-1,1}; struct node{ int x,y,tmp; }; queue <node> q; int BFS(int sx,int sy,int ex,int ey){ node str{sx,sy,0}; vis[sx][sy]=1; q.push(str); while (!q.empty()){ node f=q.front(); q.pop(); if (f.x==ex && f.y==ey) return f.tmp; for (int i=0;i<4;++i){ int xx=f.x+dx[i]; int yy=f.y+dy[i]; if (!xx || !yy || xx>n || yy>n || a[xx][yy]=='1' || vis[xx][yy]) continue; node now{xx,yy,f.tmp+1}; q.push(now); vis[xx][yy]=1; } } return -1; } int main(){ cin>>n; for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) cin>>a[i][j]; cin>>sx>>sy>>ex>>ey; cout<<BFS(sx,sy,ex,ey); return 0; } ```
by yuechenxi130407 @ 2024-10-25 20:34:09


是不是判断条件漏了
by yuechenxi130407 @ 2024-10-25 20:36:11


|