48分求调
查看原帖
48分求调
1122756
GekhubWZF楼主2025/1/3 18:03
#include <bits/stdc++.h>
using namespace std;
int a[35][35];
int mk[35][35];
int n;
int x, y;
typedef pair<int, int>PII;
int xy[4][2] = {(1, 0), (-1, 0), (0, -1), (0, 1)};
void bfs(int x, int y) {
	queue <PII>q;
	q.push(make_pair(x, y));
	while (q.size()) {
		PII f = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int xx = f.first + xy[i][0], yy = f.second + xy[i][1];
			if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && !mk[xx][yy] && !a[xx][yy]) {
				mk[xx][yy] = 1;
				q.push(make_pair(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++) {
			if (i == 1 || i == n || j == 1 || j == n ) {
				if (!a[i][j]) {
					bfs(i, j);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (mk[i][j]) {
				cout << 0 ;
			} else if (a[i][j]) {
				cout << 1;
			} else {
				cout << 2 ;
			}
			cout << " ";
		}
		cout << endl;
	}

	return 0;
}

2025/1/3 18:03
加载中...