1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| #include <iostream> #include <vector> #include <string> #include <queue>
using namespace std;
const int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1}; const int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1}; const int INF = 1e9;
int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);
int H, W; if (!(cin >> H >> W)) return 0;
vector<string> S(H); bool has_black = false; bool has_white = false;
for (int i = 0; i < H; ++i) { cin >> S[i]; for (int j = 0; j < W; ++j) { if (S[i][j] == '#') has_black = true; if (S[i][j] == '.') has_white = true; } }
if (!has_black || !has_white) { for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cout << '.'; } cout << "\n"; } return 0; }
vector<vector<int>> dist_from_black(H, vector<int>(W, INF)); vector<vector<int>> dist_from_white(H, vector<int>(W, INF)); queue<pair<int, int>> q_black, q_white;
for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (S[i][j] == '#') { dist_from_black[i][j] = 0; q_black.push({i, j}); } else { dist_from_white[i][j] = 0; q_white.push({i, j}); } } }
while (!q_black.empty()) { auto [r, c] = q_black.front(); q_black.pop();
for (int dir = 0; dir < 8; ++dir) { int nr = r + dr[dir]; int nc = c + dc[dir]; if (nr >= 0 && nr < H && nc >= 0 && nc < W) { if (dist_from_black[nr][nc] == INF) { dist_from_black[nr][nc] = dist_from_black[r][c] + 1; q_black.push({nr, nc}); } } } }
while (!q_white.empty()) { auto [r, c] = q_white.front(); q_white.pop();
for (int dir = 0; dir < 8; ++dir) { int nr = r + dr[dir]; int nc = c + dc[dir]; if (nr >= 0 && nr < H && nc >= 0 && nc < W) { if (dist_from_white[nr][nc] == INF) { dist_from_white[nr][nc] = dist_from_white[r][c] + 1; q_white.push({nr, nc}); } } } }
for (int i = 0; i < H; ++i) { string ans_row = ""; for (int j = 0; j < W; ++j) { int d = 0; if (S[i][j] == '.') { d = dist_from_black[i][j]; } else { d = dist_from_white[i][j] - 1; }
if (d % 2 == 0) { ans_row += '#'; } else { ans_row += '.'; } } cout << ans_row << "\n"; }
return 0; }
|