大佬求调,只有20分
查看原帖
大佬求调,只有20分
701120
mab123456楼主2024/12/4 01:39
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
typedef pair<int,int>pii;
ll n,m,x,y;
const ll N=405;
ll a[N][N],mark[N][N],dx[8]={-2,-2,-1,-1,1,1,2,2},dy[8]={1,-1,2,-2,2,-2,1,-1};
void bfs()
{
	memset(mark,-1,sizeof(mark));
	mark[x][y]=0;
	queue<pii>q;
	q.push({x,y});
	while(!q.empty())
	{
		pii top=q.front();
		for(int i=0;i<8;i++)
		{
			int nex=top.first+dx[i],ney=top.second+dy[i];
			if(nex>=1&&nex<=m&&ney>=1&&ney<=n&&mark[nex][ney]==-1)
			{
				mark[nex][ney]=mark[top.first][top.second]+1;
				q.push({nex,ney});
			}
		 }
		 q.pop();
	}
 } 
int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m>>x>>y;
	bfs();
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		cout<<mark[i][j]<<" ";
	    cout<<endl;
	}
	return 0;
}
2024/12/4 01:39
加载中...