WA 10pts求调
查看原帖
WA 10pts求调
661135
Mr_Gengar楼主2022/9/22 07:08
#include<bits/stdc++.h>
using namespace std;

struct node
{
	int x, y, step;
}tmp;

inline bool judge(int, int); 

queue<node> q;
const int xx[8]={0,-1,0,1,-1,-1,1,1}, yy[8]={-1,0,1,0,-1,1,1,-1};
int n, m, x, y, gx, gy;
bool vis[1005][1005], flag;
char a[1005][1005];
int main()
{
	cin >> n >> m;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=m; j++)
		{
			cin >> a[i][j];	
		}	
	}	
	while(true)
	{
		flag = false;
		memset(vis, false, sizeof(vis));
		cin >> x >> y >> gx >> gy;
		if(!x)
		{
			break;
		}
		tmp.x = x, tmp.y = y;
		q.push(tmp);
		while(!q.empty())
		{
			tmp = q.front();
			q.pop();
			if(judge(tmp.x, tmp.y))
			{
				cout << tmp.step << endl;
				while(!q.empty())
				{
					q.pop();
				}
				flag = true;
				break;
			}
			int px, py;
			for(int i=0; i<4; i++)
			{
				px = tmp.x+xx[i], py = tmp.y+yy[i];
				if(px>0&&py>0&&px<=n&&py<=m&&!vis[px][py]&&a[px][py]!='X')
				{
					q.push({px, py, tmp.step+1});
					vis[px][py] = true;
				}
			}
		}
		if(!flag)
		{
			cout << "Poor Harry" << endl;
		}
	}
	return 0;
} 

inline bool judge(int x, int y)
{
	int px, py;
	for(int i=0; i<8; i++)
	{
		px=x, py=y;
		while(px>0&&py>0&&px<=n&&py<=m&&a[px][py]!='X')
		{
			px+=xx[i], py+=yy[i];
			if(px==gx&&py==gy)
			{
				return true;
			}
		}
	}
	return false;
}
2022/9/22 07:08
加载中...