样例过了就20分捏,求助
查看原帖
样例过了就20分捏,求助
1034242
RingTouSou楼主2023/10/23 20:14
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define endl "\n"
const int N=410;

pair<int,int> q[N*N];
int n,m;
int dist[N][N];

int dx[]={2,2,1,1,-1,-1,-2,-2};
int dy[]={1,-1,2,-2,2,-2,1,-1};

void bfs(int x1,int x2)
{
	memset(dist,-1,sizeof(dist));
	int hh=0,tt=-1;
	//q[0]= {x1,x2};
	q[++tt]={x1,x2};
	dist[x1][x2]=0;

	
	while(hh<=tt)   //队头要小于等于队尾
	{
		auto t=q[hh++];   //q.front() q.pop()
		for(int i=0;i<8;i++)
		{
			int a=t.x+dx[i];
			int b=t.y+dy[i];
			
			if(a<1||a>n||b<1||b>m)
			{
				continue;
			}
			
			if(dist[a][b]>=0) continue;
			
			dist[a][b]=dist[t.x][t.y]+1;
			q[++tt]={a,b};
	}
}}

int main()
{
	int x_1,y_1;
	cin>>n>>m>>x_1>>y_1;
	bfs(x_1,y_1);
	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cout<<left<<setw(5)<<dist[i][j];
		}
		cout<<endl;
	}
	return 0;
}
2023/10/23 20:14
加载中...