AC不了一点
查看原帖
AC不了一点
1308662
Tiks_code楼主2024/3/19 18:23
#include <bits/stdc++.h>

using namespace std;

const int N = 40;

int n;

bool st[N][N];

int a[N][N] = { 0 };

typedef pair<int, int> PII;

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

queue <PII> q;

void bfs(int x, int y) {
	q.push(PII{ x,y });
	st[x][y] = true;
	while (!q.empty()) {
		PII p = q.front();
		q.pop();
		int x = p.first, y = p.second;
		for (int i = 0; i < 4; i++) {
			int tx = x + dx[i];
			int ty = y + dy[i];
			if (st[tx][ty]) continue;
			if (tx < 1 || tx > n || ty < 1 || ty > n) continue;

			st[tx][ty] = true;
			bfs(tx, ty);
		}
	}
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
			if(a[i][j]){
				st[i][j] = true;
			}
		}
	}

	int bn[][2] = { {1,1},{1,n},{n,1},{n,n} };
	for (int i = 0; i <= 3; i++) {
		 bfs(bn[i][0], bn[i][1]);
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (!st[i][j]) a[i][j] = 2;
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			printf("%-2d",a[i][j]);
		}
		cout << endl;
	}
	return 0;
}

怎么都过不去3,4,6,真的看不出问题呀呜呜

2024/3/19 18:23
加载中...