求助,样例能过,提交全WA
查看原帖
求助,样例能过,提交全WA
638832
XXCCVV楼主2022/7/16 14:09
#include<iostream>
#include<queue>
using namespace std;
struct st {
	int x,y,step;
} a;
char map[1005][1005];int vis[1005][1005];
int n,x1,y1,x2,y2;
short dex[4]={0,1,0,-1};
short dey[4]={1,0,-1,0};
void bfs() {
	queue<st>q;
	q.push(a);
	while(!q.empty()) {
		st temp=q.front();
		q.pop();
		if(temp.x==x2&&temp.y==y2) {
			cout<<temp.step;
			return;
		}
		for(int i=0;i<4;i++){
			int dx=temp.x+dex[i];
			int dy=temp.y+dey[i];
			if(dx>=1&&dx<=n&&dy>=1&&dy<=n&&map[dx][dy]=='0'&&!vis[dx][dy]){
				st tt={dx,dy,temp.step+1};
				vis[dx][dy]=1;
				q.push(tt);
			}
		}
	}
}
int main() {
	cin>>n;
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=n; j++) {
			cin>>map[i][j];
		}
	}
	cin>>x1>>y1>>x2>>y2;
	a.x=a.y=1;
	a.step=0;
	vis[1][1]=1;
	bfs();
	return 0;
}
2022/7/16 14:09
加载中...