|
| 1 | +#include "tools.h" |
| 2 | +#include <bits/stdc++.h> |
| 3 | + |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +#define ADD(a, b) pair<int, int>(a.first + b.first, a.second + b.second) |
| 8 | + |
| 9 | +class Solution |
| 10 | +{ |
| 11 | +public: |
| 12 | + vector<vector<char>> updateBoard(vector<vector<char>> &board, vector<int> &click) |
| 13 | + { |
| 14 | + int x = click[0], y = click[1]; |
| 15 | + if (board[x][y] == 'M') { |
| 16 | + board[x][y] = 'X'; |
| 17 | + return board; |
| 18 | + } |
| 19 | + queue<pair<int, int>> q; |
| 20 | + int m = board.size(), n = board[0].size(); |
| 21 | + vector<pair<int, int>> poss = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; |
| 22 | + vector<vector<bool>> flag(m, vector<bool>(n, false)); |
| 23 | + q.push({x, y}); |
| 24 | + flag[x][y] = true; |
| 25 | + int next_x, next_y, count; |
| 26 | + while (!q.empty()) |
| 27 | + { |
| 28 | + pair<int, int> c = q.front(); |
| 29 | + q.pop(); |
| 30 | + x = c.first; |
| 31 | + y = c.second; |
| 32 | + count = 0; |
| 33 | + for (const pair<int, int> &p : poss) |
| 34 | + { |
| 35 | + next_x = p.first + x; |
| 36 | + next_y = p.second + y; |
| 37 | + if (check(next_x, next_y, m, n)) { |
| 38 | + if (board[next_x][next_y] == 'M') ++count; |
| 39 | + } |
| 40 | + } |
| 41 | + // printf("%d, %d, %d\n", next_x, next_y, count); |
| 42 | + if (count==0) board[x][y] = 'B'; |
| 43 | + else board[x][y] = static_cast<char>(count + '0'); |
| 44 | + if (board[x][y] == 'B') |
| 45 | + { |
| 46 | + for (const pair<int, int> &p : poss) |
| 47 | + { |
| 48 | + next_x = p.first + x; |
| 49 | + next_y = p.second + y; |
| 50 | + |
| 51 | + if (check(next_x, next_y, m, n) && board[next_x][next_y] == 'E' && flag[next_x][next_y]==false) |
| 52 | + { |
| 53 | + q.push({next_x, next_y}); |
| 54 | + flag[next_x][next_y] = true; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return board; |
| 60 | + } |
| 61 | + |
| 62 | + inline bool check(int x, int y, int m, int n) |
| 63 | + { |
| 64 | + return x >= 0 && x < m && y >= 0 && y < n; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | + |
| 69 | +int main(int argc, char const *argv[]) |
| 70 | +{ |
| 71 | + int m, n, x, y; |
| 72 | + char c; |
| 73 | + Solution s; |
| 74 | + freopen("test.in", "r", stdin); |
| 75 | + cin >> m >> n; |
| 76 | + vector<vector<char>> matrix(m, vector<char>(n)); |
| 77 | + for (int i = 0; i < m; ++i) { |
| 78 | + for (int j = 0; j < n; ++j) { |
| 79 | + cin >> c; |
| 80 | + matrix[i][j] = c; |
| 81 | + } |
| 82 | + // PRINT_VECTOR(matrix[i], char); |
| 83 | + } |
| 84 | + cin >> x >> y; |
| 85 | + vector<int> begin = {x, y}; |
| 86 | + auto res = s.updateBoard(matrix, begin); |
| 87 | + PRINT_MATRIX(res, char, cout); |
| 88 | + return 0; |
| 89 | +} |
0 commit comments