上网查过,玄学错误(对于我来说,详见代码)
查看原帖
上网查过,玄学错误(对于我来说,详见代码)
485326
lao_da_ye楼主2022/11/8 18:52

蒟蒻写了一百多行TAT,在int Nowx=q.front().nx; 和 int Nowy=q.front().ny;出现的问题,我的数据无法正确赋值给Nowx和Nowy。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
using namespace std;
struct se
{
	int nx;
	int ny;
	int step;
	se(int a, int b, int c)
	{
		nx = a;
		ny = b;
		step = c;
	}
};
string mp[2000];
bool pd[2000][2000];
int n,m;
int ans[6000],cnt;
int stepx[5]={0,-1,0,1,0};
int stepy[5]={0,0,-1,0,1};
int fx[10]={0,-1,0,1,1,1,0,-1,-1};
int fy[10]={0,-1,-1,-1,0,1,1,1,0};
queue<se> q;

int see(int hx,int hy,int x,int y)
{
	if(hx==x&&hy==y)
	{
		return 1;
	}
	for(int i=1;i<=8;i++)
	{
		int x0=hx+fx[i];
		int y0=hy+fy[i];
		while(x0+fx[i]>=0
			  &&x0+fx[i]<n
			  &&y0+fy[i]>=0
			  &&y0+fy[i]<m
			  &&mp[x0+fx[i]][y0+fy[i]]=='O')
			  {
			  	if(x0==x&&y0==y)
			  	{
			  		return 1;
			  	}
			  	x0+=fx[i];
			  	y0+=fy[i];
			  }
	}
	return 0;
}

int bfs(int jx,int jy,int hx,int hy)
{
	pd[hx][hy]=true;
	q.push(se(hx,hy,0));
	while(!q.empty())
	{
		int Nowx=q.front().nx;
		int Nowy=q.front().ny;
		q.pop();
		if(see(Nowx,Nowy,jx,jy))
		{
			return q.front().step;
		}
		for(int i=1;i<=4;i++)
		{
			if(Nowx+stepx[i]>=0
			   &&Nowx+stepx[i]<n
			   &&Nowy+stepy[i]>=0
			   &&Nowy+stepy[i]<m
			   &&pd[Nowx+stepx[i]][Nowy+stepy[i]]==false
			   &&mp[Nowx+stepx[i]][Nowy+stepy[i]]=='O')
			{
				q.push({Nowx+stepx[i],Nowx+stepx[i],q.front().step+1});
				pd[Nowx+stepx[i]][Nowy+stepy[i]]=true;
			}
		}
	}
	return -1;
}

int main()
{
	int jx,jy,hx,hy;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		cin>>mp[i];
	}
	while(1)
	{
		memset(pd,false,sizeof(pd));
		cin>>jx>>jy>>hx>>hy;
		while(!q.empty())
		{
			q.pop();
		}
		if(jx==0&&jy==0&&hx==0&&hy==0)
		{
			break;
		}
		jx--;
		jy--;
		hx--;
		hy--;
		if(hx==jx&&hy==jy)
		{
			cnt++;
			ans[cnt]=0;
		}
		else
		{
			cnt++;
			ans[cnt]=bfs(hx,hy,jx,jy);
		}
	}
	for(int i=1;i<=cnt;i++)
	{
		if(ans[i]!=-1)
		{
			cout<<ans[i]<<endl;
		}
		else
		{
			cout<<"Poor Harry"<<endl;
		}
	}
	return 0;
}
2022/11/8 18:52
加载中...