90分求助
查看原帖
90分求助
1036965
bjqxszx_zhuzixuan楼主2024/5/18 16:09
#include<bits/stdc++.h>
using namespace std;
queue<int> q,p;
int a[1001][1001],s[1001][1001],tx1,ty1,tx[101]={0,-2,-2,-1,-1,1,1,2,2},ty[101]={0,-1,1,-2,2,-2,2,-1,1},n,m,x,y;
int main() 
{
	cin>>n>>m>>x>>y;
	q.push(x);
	p.push(y);
	a[x][y]=1;
	while(!q.empty())
	{
		
		for(int i=0;i<=8;i++)
		{
			tx1=q.front()+tx[i],ty1=p.front()+ty[i];
			if(tx1>0&&tx1<=n&&ty1>0&&ty1<=m)
			{
				if(!a[tx1][ty1])
			    {
			    	a[tx1][ty1]=1;
			    	s[tx1][ty1]=s[q.front()][p.front()]+1;
			    	q.push(tx1);
			    	p.push(ty1);
				}
			}
			
		}
		q.pop();
		p.pop();
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
	    {
	        if(a[i][j])
	    		printf("%-5d",s[i][j]);
	    	else
	    	cout<<"    -1";
		}
		cout<<endl;
	}
}
2024/5/18 16:09
加载中...