大佬大佬,我错哪了~(^_^)~
查看原帖
大佬大佬,我错哪了~(^_^)~
635737
TaoTi楼主2022/2/7 15:01
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
const int dx[4]={0,0,-1,1};
const int dx[4]={-1,1,0,0};
struct node{
    int x,y,dist;
};
int n,x1,yy,x2,y2;
bool h[N][N],flag[N][N];
queue<node>q;
void bfs(){
    flag[x1][yy]=true;
    node tmp;
    tmp.x=x1;
    tmp.y=yy;
    tmp.dist=0;
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        for(int i=0;i<=3;i++){
            int tx=tmp.x+dx[i];
            int ty=tmp.y+dy[i];
            if(tx>0&&tx<=n&&ty>=0&&ty<=n&&h[tx][ty]&&!flag[tx][ty]){
                flag[tx][ty]=true;
                node tn;
                tn.x=tx;
                tn.y=ty;
                tn.dist=tmp.dist+1;
                q.push(tn);
                if(tx==x2&&ty==y2){
                    cout<<tn.dist;
                    return;
                }
            }
        }
    }
}
int main(){
    char ch;
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            cin>>ch;
            if(ch=='0')h[i][j]=true;
        }
    cin>>x1>>yy>>x2>>y2;
    bfs();
    return 0;
}
2022/2/7 15:01
加载中...