求助代码大蛇
查看原帖
求助代码大蛇
1475851
kmszszyc2024楼主2025/1/20 15:38
#include <iostream>
#include <queue>
using namespace std;
const int N = 200;
int a[N][N];
int n;

struct qq {
	int p, q;
};
queue<qq>st;

int dx[4] = {-1, 0, 1, 0};

int dy[4] = {0, 1, 0, -1};

void bfs(int x, int y) {
	a[x][y] = 6;
	st.push(qq{x, y});

	while (!st.empty()) {
		x = st.front().p;
		y = st.front().q;
		st.pop();

		for (int i = 0; i < 4; i++) {

			int xx = x + dx[i];
			int yy = y + dy[i];

			if (xx < 1 || xx > n || yy < 1 || yy > n) {
				continue;
			}

			if (a[xx][yy] == 1) {
				continue;
			}

			a[xx][yy] = 6;
			st.push(qq{xx, yy});
		}
	}
}

int main() {

	cin >> n;

	for (int i = 1; i <= n; i++) {

		for (int j = 1; j <= n; j++) {

			cin >> a[i][j];
		}
	}

	for (int i = 1; i <= n; i++) {

		for (int j = 1; j <= n; j++) {

			bfs(i, j);
		}
	}

	for (int i = 1; i <= n; i++) {

		for (int j = 1; j <= n; j++) {

			if (a[i][j] == 0)
				a[i][j] = 2;

		}

		for (int j = 1; j <= n; j++) {

			if (a[i][j] == 6)
				a[i][j] = 0;

		}
	}


	for (int i = 1; i <= n; i++) {

		for (int j = 1; j <= n; j++) {

			cout << a[i][j] << " ";
		}

		cout << "\n";
	}

	return 0;
}
2025/1/20 15:38
加载中...