32分求助
查看原帖
32分求助
996893
exLucas楼主2024/6/24 09:38
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 31;
int n, m, x, y,g[N][N];
PII q[N*N];
void bfs(){
    int hh = 0, tt = -1;
    q[++tt] = {0, 0};
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
    while (hh <= tt){
        PII t = q[hh++];
        for (int i = 0; i < 4;++ i){
            x = t.first + dx[i], y = t.second + dy[i];
            if (x >= 0 && x < n && y >= 0 && y < n && g[x][y] == 0){
                g[x][y] = -1;
                q[++tt]= {x, y};
            }
        }
    }
}
int main(){
	scanf("%d", &n);
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < n; j ++ )
			scanf("%d", &g[i][j]);
    bfs();
    for (int i = 0; i < n; i ++ ){
        for (int j = 0; j < n; j ++ ){
			if(g[i][j] == -1) printf("0 ");
			else if(g[i][j] == 1) printf("1 ");
			else printf("2 ");
		}
		puts("");
	}
    return 0;
}
2024/6/24 09:38
加载中...