全Wa求助
查看原帖
全Wa求助
1299513
zzy0816楼主2024/3/21 19:50
#include <bits/stdc++.h>
using namespace std;
int x1[9]={1,1,-1,-1,2,2,-2,-2};
int x2[9]={2,-2,2,-2,-1,1,-1,1};
struct node
{
	int x,y,step;
};
queue < node > q;
bool vis[405][405]={0};
int md[405][405];
int main()
{
	for(int i=0;i<=405;i++) for(int j=0;j<=405;j++) md[i][j]=-1;
	int m,n,x,y;
	cin>>m>>n>>x>>y;
	q.push({x-1,y-1,0});
	while(!q.empty())
	{
		node num=q.front();
		q.pop();
		md[num.x][num.y]=num.step;
		for(int i=0;i<8;i++)
		{
			int x=num.x,y=num.y;
			int step=num.step;
			x+=x1[i];
			y+=x2[i];
			if(x>=0 and x<m and y>=0 and y<n and md[x][y]==-1 and vis[x][y]==false)
			{
				q.push({x,y,(step)+1});
				vis[x][y]=true;
			}
			
		}
	 } 
	 for(int i=0;i<m-1;i++) 
	 {
	 	for(int j=0;j<n;j++) 
		 {
		 	printf("%-5d",md[i][j]);
		 }
	 	cout<<endl;
	}
	for(int j=0;j<n;j++) 
	{
		printf("%-5d",md[m-1][j]);
	}
	return 0;
 } 
2024/3/21 19:50
加载中...