WA56求助
查看原帖
WA56求助
1041884
_Liyx_楼主2024/7/10 18:22

调了一个小时还没挑出来,求各位大佬帮忙看看

#include <bits/stdc++.h>
using namespace std;
int n,m;
string a[16400];
string mp[16400];
bool vis[16400]={};
int sx,sy,ex,ey;
int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,1,0,-1,-1,1,-1,1};
struct node{
	int x,y,step;
};
void bfs(){
	queue<node> q;
	q.push((node){sx,sy,0});
	memset(vis,0,sizeof(vis));
	vis[(sx-1)*m+sy]=1;
	while(!q.empty()){
		node now=q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int xx=now.x+dx[i],yy=now.y+dy[i];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]!='X'&&vis[(xx-1)*m+yy]==0){
				if(mp[xx][yy]=='e'){
					cout<<now.step+1<<"\n";
					return;
				}
				vis[(xx-1)*m+yy]=1;
				q.push((node){xx,yy,now.step+1});
			}
		}
	}
	cout<<"Poor Harry\n";
	return;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i]="0"+a[i];
	}
	while(1){
		cin>>ex>>ey>>sx>>sy;
		if(!sx&&!sy&&!ex&&!ey) break;
		if(ex==sx&&ey==sy){
			cout<<"0\n";
			continue;
		}
		for(int i=1;i<=n;i++) mp[i]=a[i];
		for(int i=0;i<8;i++){
			int x=ex,y=ey;
			mp[x][y]='e';
			while(1){
				x+=dx[i];
				y+=dy[i];
				if(x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y]=='O')
					mp[x][y]='e';
				else break;
			}
		}
		bfs();
	}
	return 0;
}
2024/7/10 18:22
加载中...