乱搞20分求助 悬赏关注
查看原帖
乱搞20分求助 悬赏关注
823773
_sh1kong_楼主2022/12/15 13:37

悬赏关注

#include<iostream>
#include<cstring>
#include<queue>
#include<climits>
#define int long long
#define MAXN 2001
using namespace std;
typedef char ch;
int n,m;
int harry_x,harry_y,cupx,cupy;
bool map[MAXN][MAXN],cup[MAXN][MAXN];
struct node{
	int x,y,step;
};
queue <node> q;
int pyx[5]={0,0,1,-1},pyy[5]={1,-1,0,0};
int lx[8]={-1,-1,-1,0,0,1,1,1},
	ly[8]={0,1,-1,-1,1,-1,0,1};
void cup_range(int cupx,int cupy)
{
	cup[cupx][cupy]=1;
	for(int i=0;i<8;i++)
	{
		int xx=cupx,yy=cupy;
		while(1)
		{
			xx+=lx[i],yy+=ly[i];
			if(!(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!cup[xx][yy]&&!map[xx][yy])) break;
			cup[xx][yy]=1;
		}
	}
}
short bfs()
{
	while(!q.empty())
	{
		node s=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			int xx=s.x+pyx[i],yy=s.y+pyy[i];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!map[xx][yy])
			{
				map[xx][yy]=1;
				node k;
				k.x=xx;k.y=yy;k.step=s.step+1;
				if(cup[xx][yy])
				{
					cout<<k.step<<endl;
					while(!q.empty()) q.pop();
					return 0;
				}
				q.push(k);
			}
		}
	}
	return -1;
}
signed main()
{
	//freopen("Harry.in","r",stdin);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			ch temp;
			cin>>temp;
			temp=='O'?map[i][j]=0:map[i][j]=1;
		}
	}
	/*
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++) cout<<map[i][j]<<" ";
		cout<<endl;
	}
	*/
	cin>>cupx>>cupy>>harry_x>>harry_y;
	while(harry_x+harry_y+cupx+cupy!=0)
	{
		memset(cup,0,sizeof(cup));
		cup_range(cupx,cupy);
		/*
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)  cout<<cup[i][j]<<" ";
			cout<<"\n";
		}
		*/
		node p;
		p.x=harry_x;p.y=harry_y;p.step=0;
		q.push(p);
		map[harry_x][harry_y]=1;
		if(bfs()==-1) cout<<"Poor Harry"<<endl;
		cin>>cupx>>cupy>>harry_x>>harry_y;
	} 
	return 0;
}
2022/12/15 13:37
加载中...