8re,2wa是怎么回事
查看原帖
8re,2wa是怎么回事
1036597
ESxyz楼主2023/7/12 14:23
#include <bits/stdc++.h>
using namespace std;
char aa[1005];
struct xy{
    int x,y;
}node,Top;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,-0,1,-1};
int a[401][401];
bool b[401][401];
int n,m;
void bfs(int x,int y,int step){
	if(x==n&&y==m)
	return;
    a[x][y] = step;
    b[x][y] = true;
    queue<xy> Q;
    node.x = x;
    node.y = y;
    Q.push(node);
    while (!Q.empty()){
        Top=Q.front();
        Q.pop();
            for (int i=0;i<4;i++){
                        int NewX=Top.x+dx[i];
                        int NewY=Top.y+dy[i];
                        if (NewX<1||NewX>n||NewY<1||NewY>m) continue;
                        if (!b[NewX][NewY]){
                        	a[NewX][NewY]=a[Top.x][Top.y]+1;
                        	b[NewX][NewY]=true;
                            node.x=NewX;
                            node.y=NewY;
                            Q.push(node);
                            
                        }
                    }
    }
}
int main(){
    int x,y;
    cin>>n;
    for(int zx=0;zx<n;zx++){
    	cin>>aa;
    	for(int zy=0;zy<n;zy++)if(aa[zy]=='1')b[zx+1][zy+1]=true;
	}
	cin>>x>>y>>n>>m;
    bfs(x,y,0);
    cout<<a[n][m];
    
    return 0;
}
2023/7/12 14:23
加载中...