32 SOS
查看原帖
32 SOS
1014170
pocaogaozhi楼主2023/7/15 16:00
#include <bits/stdc++.h>
using namespace std;
int n;
int a[40][40];
int b[40][40];
int xf[4] = {1, -1, 0, 0};
int yf[4] = {0, 0, -1, 1};
void dfs(int x, int y) {

	if (x >= 1 && x <= n && y >= 1 && y >= n && b[x][y] == 0) {
		b[x][y] = 1;
		for (int i = 0; i < 4; i++) {
			
			if (a[x + xf[i]][y + yf[i]] == 0) {
				
				dfs(x + xf[i], y + yf[i]);
				
			}
			
		}
	}
}


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

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

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

	}

	dfs(1, 1);
	dfs(1, n);
	dfs(n, 1);
	dfs(n, n);

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

		for (int j = 1; j <= n; j++) {
			if (b[i][j] == 0) a[i][j] = 2;
			cout << a[i][j] << " ";
		}
		cout << endl;
	}

}
2023/7/15 16:00
加载中...