全RE,本人萌新,赏关
查看原帖
全RE,本人萌新,赏关
1564862
j66666666j楼主2025/1/18 16:37
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
struct coord
{
	int x;
	int y;
};
int qo[40][40];
int wk[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };
queue<coord>Q;
bool visited[40][40];
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			scanf("%d", &qo[i][j]);
			visited[i][j] = false;
		}
	}

	for (int i = 0; i < n; i++)
	{
		if (qo[i][0] == 0) {
			Q.push({ i, 0 });
			visited[i][0] = true;
		}
		if (qo[i][n - 1] == 0) {
			Q.push({ i, n - 1 });
			visited[i][n - 1] = true;
		}
	}
	for (int j = 0; j < n; ++j) {
		if (qo[0][j] == 0) {
			Q.push({ 0, j });
			visited[0][j] = true;
		}
		if (qo[n - 1][j] == 0) {
			Q.push({ n - 1, j });
			visited[n - 1][j] = true;
		}
	}
	while (!Q.empty())
	{

		coord u = Q.front();
		int ux = u.x;
		int uy = u.y;
		Q.pop();
		for (int k = 0; k < 4; k++)
		{
			int x = ux + wk[k][0];
			int y = uy + wk[k][1];
			if (x >= 0 && y >= 0 && x < n && y < n && (!visited[x][y]))
			{
				coord t = { x,y };
				Q.push(t);
				visited[x][y] = true;
			}


		}

	}
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (qo[i][j] == 0 && !visited[i][j]) {
				qo[i][j] = 2;
			}
		}
	}

	
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			cout << qo[i][j] << " ";
		}
		cout << endl;
	}

	return 0;
}
2025/1/18 16:37
加载中...