有WA有AC有TLE,就很离谱
查看原帖
有WA有AC有TLE,就很离谱
992988
ling_xi_楼主2023/7/27 22:33

应该看得懂吧。。。我是懒人


#include <bits/stdc++.h>
using namespace std;
long long int n,m,xx,yy;
int a[1002][1002];
int book[1002][1002];
struct gxz
{
	int x,y,st;
}u,z;
queue <gxz> s;
int fx[8][2]={{2,1},{2,-1},{1,-2},{1,2},{-2,-1},{-1,-2},{-2,1},{-1,2}};
void bfs(int x,int y)
{
	book[xx][yy]=1;
	u.x=xx;
	u.y=yy;
	u.st=0;
	s.push(u);
	while(!s.empty())
	{
		 u.x=s.front().x;
		 u.y=s.front().y;
		 u.st=s.front().st;
		 s.pop();
		 if(u.x==x && u.y==y)
		 {
		 	printf("%-5d",u.st);
		 	return;
		 }
		 for(int i=0;i<8;i++)
		 {
		 	int nx=u.x+fx[i][0];
		 	int ny=u.y+fx[i][1];
		 	int nstep=u.st+1;
		 	if(nx>0 && nx<=n && ny>0 && ny<=m && book[nx][ny]!=1)
		 	{
		 			z.x=nx;
		 			z.y=ny;
		 			z.st=nstep;
		 			s.push(z);
		 			book[z.x][z.y]=1;
			}
		}
	}
	printf("%-5d",-1);
}
int main()
{
	cin>>n>>m>>xx>>yy;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			u.st=0;
			memset(book,0,sizeof(book));
			memset(a,-1,sizeof(a));
			bfs(i,j);
			u.st=0;
			z.st=0;
		}
		cout<<endl;
	}
	return 0;
}
2023/7/27 22:33
加载中...