差两个测试点没过,输出到52行的时候后面就为0了不知道为什么
查看原帖
差两个测试点没过,输出到52行的时候后面就为0了不知道为什么
1069315
hm114514楼主2024/3/23 17:43
#include<queue>
#include<string>

using namespace std;

const int N = 410;
int map[N][N];
int n, m;
int x, y;

typedef pair<int, int> PII;

void bfs()
{
	PII begin{x, y};
	queue<PII> q;
	q.push(begin);
	int dx[8] = { -2,-1,1,2,2,1,-1,-2 }, dy[8] = { -1,-2,-2,-1,1,2,2,1 };
	while (!q.empty())
	{
		PII start = q.front();
		q.pop();
		for (int i = 0; i < 8; i++)
		{
			int nx = start.first + dx[i];
			int ny = start.second + dy[i];
			if (nx > 0 && nx <= n && ny > 0 && ny <= m && map[nx][ny] == -1)
			{
				q.push({ nx,ny });
				map[nx][ny] = map[start.first][start.second] + 1;
			}
		}
	}
}

int main()
{
	memset(map, -1, N * N);
	cin >> n >> m;
	cin >> x >> y;
	map[x][y] = 0;
	bfs();
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			cout << map[i][j] << ' ';
		}
		cout << '\n';
	}
	return 0;
}
2024/3/23 17:43
加载中...