题目链接:填涂颜色
思路
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 30 + 10;
#define ll long long
int mp[N][N], dir[5][2] = {
{ 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }
}, n;
bool vis[N][N];
bool check(int x, int y) {
return x >= 0 && x <= n + 1 && y >= 0 && y <= n + 1;
}
bool BFS(int x, int y) {
queue<pair<int, int>> q;
q.push({x, y});
while (!q.empty()) {
pair<int, int> point = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
pair<int, int> dpoint = point;
dpoint.first = point.first + dir[i][0];
dpoint.second = point.second + dir[i][1];
if (check(dpoint.first, dpoint.second)) {
if (mp[dpoint.first][dpoint.second] == 0) {
mp[dpoint.first][dpoint.second] = 3;
q.push(dpoint);
}
}
}
}
return true;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> mp[i][j];
}
}
BFS(0, 0);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (mp[i][j] == 3) {
cout << 0 << " ";
} else if (mp[i][j] == 0) {
cout << 2 << " ";
} else {
cout << 1 << " ";
}
}
cout << endl;
}
return 0;
}
标签:dpoint,填涂,洛谷,point,int,second,mp,P1162,first
From: https://www.cnblogs.com/againss/p/18250580