aaa20'求条悬棺

P1746 离开中山路

nobody_here @ 2024-10-02 23:16:13

#include<bits/stdc++.h>
using namespace std;
char a[101][101];
int n,sx,sy,ex,ey,vs[101][101];
int ax[4]={0,0,-1,1};
int ay[4]={-1,1,0,0};
struct xml{
    int x,y,step;
};
bool check(int i,int j){
    if(i>=1&&j>=1&&i<=n&&j<=n&&a[i][j]!='1'&&vs[i][j]!=1) return true;
    else return false;
}
int bfs(){
    queue <xml> Q;
    Q.push({sx,sy,0});
    vs[sx][sy]=1;
    while(!Q.empty()){
        xml u=Q.front();
        Q.pop();
        for(int i=0;i<4;i++){
            int x=u.x+ax[i];
            int y=u.y+ay[i];
            if(check(x,y)){
                if(x==ex&&y==ey) return u.step+1;
                vs[x][y]=1;
                Q.push({x,y,u.step+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();
    return 0;
}

|