为什么全部RE,求大佬指点
查看原帖
为什么全部RE,求大佬指点
453524
TwilightSparkle楼主2021/7/31 08:36
#include<stdio.h>
#include<queue>
using namespace std;
int n,m,ans;
int map[3005][3005];
int maps[3005][3005];
bool flag[3005][3005];
int dx[9]={0,1,1,0,-1,-1,-1,0,1};
int dy[9]={0,0,1,1,1,0,-1,-1,-1};
struct node{
	int x,y,dis;
};
queue<node> q;
inline void init()
{
	for(register int i=1;i<=n;i++)
	{
		for(register int j=1;j<=m;j++)
			maps[i][j]=map[i][j];
	}
}
inline void dfs(node s)
{
	for(register int i=1;i<=8;i++)
	{
		node cur=s;
		while(1)
		{
			cur.x+=dx[i];
			cur.y+=dy[i];
			if(maps[cur.x][cur.y]==1||cur.x<=0||cur.x>n||cur.y<=0||cur.y>m)
				break;
			else
				maps[cur.x][cur.y]=2;
		}
	}
}
inline void bfs(node s)
{
	while(!q.empty())
	{
		node now=q.front();
		q.pop();
		if(maps[now.x][now.y]==2)
		{
			while(!q.empty())
				q.pop();
			ans=now.dis;
			return ;
		}
		for(register int i=1;i<=8;i++)
		{
			node nxt;
			nxt.x=now.x+dx[i];
			nxt.y=now.y+dy[i];
			if((nxt.x>0&&nxt.x<=n)&&(nxt.y>0&&nxt.y<=m)&&maps[nxt.x][nxt.y]!=1&&flag[nxt.x][nxt.y]==0)
			{
				flag[nxt.x][nxt.y]=1;
				nxt.dis=now.dis+1;
				q.push(nxt);
			}
		}
	}
}
int main()
{
	scanf("%d %d",&n,&m);
	getchar();
	char tmp;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			scanf("%c",&tmp);
			if(tmp=='X')
			{
				map[i][j]=1;				
			}
			else
				map[i][j]=0;
		}
		getchar();
	}
	while(1)
	{
		ans=-1;
		int hx,hy,px,py;
		scanf("%d%d%d%d",&hx,&hy,&px,&py);
		if(hx==0&&hy==0&&px==0&&py==0)
			break;
		else
		{
			init();
			maps[px][py]=2;
			node s1;
			s1.x=px;
			s1.y=py;
			dfs(s1);
			node s2;
			s2.x=hx;
			s2.y=hy;
			flag[hx][hy]=1;
			s2.dis=0;
			q.push(s2);
			bfs(s2);
			if(ans==-1)
				printf("Poor Harry\n");
			else
				printf("%d\n",ans);
		}
	}
	return 0;
}
2021/7/31 08:36
加载中...