不慕放糖 @ 2021-08-20 20:45:30
#include<bits/stdc++.h>
using namespace std;
int n,vis[1005][1005],mp[1005][1005];
int x1,y3,x2,y2;
struct node{
int x,y,step;
};
queue<node>q;
int xx[4]={1,0,-1,0};
int yy[4]={0,1,0,1};
int main(){
cin>>n;
char b;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>b;
if(b=='1')mp[i][j]=1;
else mp[i][j]=0;
}
}
cin>>x1>>y3>>x2>>y2;
q.push(node{x1,y3,0});
vis[x1][y3]=true;
mp[x2][y2]=1e9;
while(!q.empty()){
for(int i=0;i<4;i++){
int dx=xx[i]+q.front().x;
int dy=yy[i]+q.front().y;
if(dx==x2&&dy==y2){
// cout<<dx<<" "<<dy<<" "<<q.front().step+1<<" "<<mp[x2][y2]<<endl;
mp[x2][y2]=min(mp[x2][y2],q.front().step+1);
}
if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&!vis[dx][dy]&&mp[dx][dy]==0){
q.push(node{dx,dy,q.front().step+1});
vis[dx][dy]=1;
}
}
q.pop();
}
cout<<mp[x2][y2];
return 0;
}
by bmqt @ 2021-08-20 20:46:50
别只发代码,要把思路讲清楚来哇
by 不慕放糖 @ 2021-08-20 20:53:02
我认为自己是输入错了
by 不慕放糖 @ 2021-08-20 20:55:46
因为我把cin>>改成scanf就re了
by little_cindy @ 2021-08-21 08:21:33
@不慕放糖 是TLE还是WA
by 不慕放糖 @ 2021-08-21 08:22:13
@little_cindy WA
by little_cindy @ 2021-08-21 08:50:53
@不慕放糖 方向函数错了。
int xx[4]={1,0,-1,0};
int yy[4]={0,1,0,1};
改为
int xx[4]={1,0,-1,0};
int yy[4]={0,1,0,-1};
记住,方向函数永远等长
by little_cindy @ 2021-08-21 08:51:21
@不慕放糖 给个关注~
by 不慕放糖 @ 2021-08-21 08:53:52
@little_cindy 谢谢大佬