救救孩子我吧《P1746 离开中山路》
查看原帖
救救孩子我吧《P1746 离开中山路》
973280
Apollo0130楼主2024/7/4 09:09

WA九个

本人已达到最大能力

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int g[N][N];
bool st[N][N],f;
int dx[5]={-1,0,1,0};
int dy[5]={0,1,0,-1};
int n,sx,sy,ex,ey,sum;
struct node{
	int x,y,step;
}q[100*N];
int tt=1,hh=1,tx,ty;

void bfs(int x,int y){
	q[tt].x=sx;
	q[tt].y=sy;
	q[tt].step=0;
	tt++;
	while(hh<=tt){
		for(int i=0;i<4;i++){
			tx=q[hh].x+dx[i];
			ty=q[hh].y+dy[i];
			if(tx<1 || tx>n || ty<1 || ty>n)continue;
			if(!st[tx][ty]){
				q[tt].x=tx;
				q[tt].y=ty;
				q[tt].step=q[hh].step+1;
				st[tx][ty]=1;
				tt++;
			}
			if(tx==ex && ty==ey){
				f=1;
				cout<<q[tt-1].step;
				break;
			}
		}
		if(f)break;
		hh++;
	}
}

int main(){
	cin>>n;
	char c;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>c;
			g[i][j]=c-48;
		}
	}
	cin>>sx>>sy>>ex>>ey;
	st[sx][sy]=1;
	bfs(sx,sy);
} 
2024/7/4 09:09
加载中...