30分,菜菜求大佬改
查看原帖
30分,菜菜求大佬改
1380473
ZY_202301005129楼主2024/7/10 22:16
#include <iostream>
#include<queue>
//#include<bits/stdc++.h>
using namespace std;
int n, m, x, y;
#define N 401
int a[N][N];
bool b[N][N];
struct p
{
	int x, y, v;
};
void judge(int xx, int yy)//所求点
{
	for (int i = 1;i <= n;i++)
		memset(b[i], 0, sizeof(b[i]));//清空上一次调用的残留
	queue<p> p1;
	p p0 = { x,y,0 };p1.push(p0);
	b[y][x] = 1;
	int p11 = 1;
	while (p11 != 0)
	{
		int x0 = p1.front().x;
		int y0 = p1.front().y;
		if (x0 == xx && y0 == yy)
		{
			a[yy][xx] = p1.front().v;
			return;
		}
		if (x0 + 2 <= m && y0 + 1 <= n && !b[y0 + 1][x0 + 2])
		{
			p p0 = { x0 + 2,y0 + 1,p1.front().v + 1 };
			p1.push(p0);
			p11++;
			b[y0 + 1][x0 + 2] = 1;
		}
		if (x0 + 1 <= m && y0 + 2 <= n && !b[y0 + 2][x0 + 1])
		{
			p p0 = { x0 + 1,y0 + 2,p1.front().v + 1 };
			p1.push(p0);
			p11++;
			b[y0 + 2][x0 + 1] = 1;
		}
		if (x0 - 1 >= 1 && y0 + 2 <= n && !b[y0 + 2][x0 - 1])
		{
			p p0 = { x0 - 1,y0 + 2,p1.front().v + 1 };
			p1.push(p0);
			p11++;
			b[y0 + 2][x0 - 1] = 1;

		}
		if (x0 - 2 >= 1 && y0 + 1 <= n && !b[y0 + 1][x0 - 2])
		{
			p p0 = { x0 - 2,y0 + 1,p1.front().v + 1 };
			p1.push(p0);
			p11++;
			b[y0 + 1][x0 - 2] = 1;
		}
		if (x0 - 2 >= 1 && y0 - 1 >= 1 && !b[y0 - 1][x0 - 2])
		{
			p p0 = { x0 - 2,y0 - 1,p1.front().v + 1 };
			p1.push(p0);
			p11++;
			b[y0 - 1][x0 - 2] = 1;
		}
		if (x0 - 1 >= 1 && y0 - 2 >= 1 && !b[y0 - 2][x0 - 1])
		{
			p p0 = { x0 - 1,y0 - 2,p1.front().v + 1 };
			p1.push(p0);
			p11++;
			b[y0 - 2][x0 - 1] = 1;
		}
		if (x0 + 1 <= m && y0 - 2 >= 1 && !b[y0 - 2][x0 + 1])
		{
			p p0 = { x0 + 1,y0 - 2,p1.front().v + 1 };
			p1.push(p0);
			p11++;
			b[y0 - 2][x0 + 1] = 1;
		}
		if (x0 + 2 <= m && y0 - 1 >= 1 && !b[y0 - 1][x0 + 2])
		{
			p p0 = { x0 + 2,y0 - 1,p1.front().v + 1 };
			p1.push(p0);
			p11++;
			b[y0 - 1][x0 + 2] = 1;
		}
		p1.pop();
		p11--;
	}
	a[yy][xx] = -1;
}
int main()
{
	cin >> n >> m >> x >> y;
	for (int i = 1;i <= n;i++)//行,y的范围是1~n
		for (int j = 1;j <= m;j++)//列,x的范围是1~m
		{
			judge(j, i);
		}
	for (int i = 1;i <= n;i++)
	{
		for (int j = 1;j <= m;j++)
			cout << a[i][j] << " ";
		cout << "\n";
	}

}
2024/7/10 22:16
加载中...