求助,只AC1,2,3!!!!
查看原帖
求助,只AC1,2,3!!!!
524906
刘辰雨楼主2021/10/9 17:06
#include<bits/stdc++.h>
using namespace std;
int n,m,h_x,h_y;
int Map[10001][10001];
int X,Y;
struct point
{
	int x,y;
};
int fx[10] = {0,-1,1,2,2,1,-1,-2,-2};
int fy[10] = {0,2,2,1,-1,-2,-2,-1,1};
queue<point>q;
int pd(int x , int y)
{
	if(x<1||x>n)return false;
	if(y<1||y>m)return false;
	else if(x==h_x && y==h_y)return false;
	else if(Map[x][y] == 0)return true;
	else return false;
}
int main()
{
//	freopen("P1443_4.in","r",stdin);
//	freopen("1.out","w",stdout);
	cin>>n>>m>>h_x>>h_y;
	point tp = {h_x , h_y};
	int x1 = h_x;
	int y1 = h_y;
	q.push(tp);
	Map[h_x][h_y] = 0;
	//cout<<h_x<<" "<<h_y<<endl; 
	while(!q.empty())
	{
		x1 = q.front().x;
		y1 = q.front().y;
		for(int u = 1 ; u <= 8 ; u++ )
		{
			
			X =  x1 + fx[u];
			Y =  y1 + fy[u];
			if(pd(X,Y))
			{
				Map[X][Y] = Map[x1][y1] + 1;
				tp = {X,Y};
				q.push(tp);
			}
		}
		q.pop();
	}
	for(int i = 1 ; i <= n ; i++ )
	{
		for(int k = 1 ; k <= m ; k++ )
		{
			if(Map[i][k] == 0&&!(i == h_x && k == h_y))
			{
				cout<<"-1"<<"   ";
				continue;
			}
			cout<<Map[i][k]<<"    ";
		}
		cout<<endl;
	}
//	fclose(stdin);
//	fclose(stdout);
	return 0;
}
2021/10/9 17:06
加载中...