30分,每次都错误的输出Poor Harry
查看原帖
30分,每次都错误的输出Poor Harry
448965
杨丶老爹楼主2021/8/22 09:37

RT

孩子改了一上午了,是在没看出哪错了

#include<bits/stdc++.h>
#define text cout<<"-------------------"<<endl;
#define work for(int i=0;i<=n+1;i++){for(int j=0;j<=m+1;j++){cout<<mp[i][j];}cout<<endl;}
using namespace std;
const int N=2000;
char mp[N][N];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int lookx[8]={-1,-1,-1,0,0,1,1,1};
int looky[8]={0,1,-1,-1,1,-1,0,1};
bool vis[N][N],p[N][N];
int n,m,sx,sy,fx,fy,dis[N*N];
int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))
	{
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
void position()
{
	memset(p,0,sizeof(p));
	p[fx][fy]=1;
	for(int i=0;i<8;i++)
	{
		int x=fx+lookx[i],y=fy+looky[i];
		while(mp[x][y]!='X')
		{
			p[x][y]=1;
			x+=lookx[i];
			y+=looky[i];
		}
	}
}
struct node{
    int x,y;
}q[N*N];
void bfs()
{
	q[1].x=sx,q[1].y=sy;
	int head=0,tail=1;
	memset(vis,0,sizeof(vis));
	dis[0]=0;vis[sx][sy]=1;
	while(head<tail)
	{
		head++;
		if(p[q[head].x][q[head].y])
		{
			printf("%d\n",dis[head]);
			return;
		}
		for(int i=0;i<3;i++)
		{
			int nx=q[head].x+dx[i],ny=q[head].y+dy[i];
			if(mp[nx][ny]=='O' && !vis[nx][ny])
			{
				q[++tail].x=nx;
				q[tail].y=ny;
				dis[tail]=dis[head]+1;
				vis[nx][ny]=1;
			}
		}
	}
	printf("Poor Harry\n");
	return;
}
int main()
{
	for(int i=0;i<N;i++)
		for(int j=0;j<N;j++)
			mp[i][j]='X';
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>mp[i][j];
	fx=read();fy=read();sx=read();sy=read();
	while(fx!=0)
	{
		position();
//		for(int i=1;i<=n;i++)
//		{
//			for(int j=1;j<=m;j++)
//				cout<<p[i][j]<<' ';
//			cout<<endl;
//		}
		bfs();
		fx=read();fy=read();sx=read();sy=read();
	}
	return 0;
}

2021/8/22 09:37
加载中...