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
| queue<pair<int, int>> q; void bfsColor(vector<vector<char>>& board,int len,int width) { while (!q.empty()) { pair<int, int> tmp = q.front(); q.pop(); int i = tmp.first; int j = tmp.second; board[i][j] = 'Y'; if ((i - 1 >= 0) && (board[i - 1][j] == 'O')) q.push(pair<int, int>(i - 1, j)); if ((i + 1 < len) && (board[i + 1][j] == 'O')) q.push(pair<int, int>(i + 1, j)); if ((j - 1 >= 0) && (board[i][j - 1] == 'O')) q.push(pair<int, int>(i, j - 1)); if ((j + 1 < width) && (board[i][j + 1] == 'O')) q.push(pair<int, int>(i, j + 1)); } } void solve(vector<vector<char>>& board) { int len = board.size(); if (!len)return; int width = board[0].size(); if (!width)return; for (int i = 0; i < len; i++) { if (board[i][0] == 'O') q.push(pair<int, int>(i, 0)); if (board[i][width - 1] == 'O') q.push(pair<int, int>(i, width - 1)); } for (int i = 0; i < width; i++) { if (board[0][i] == 'O') q.push(pair<int, int>(0, i)); if (board[len - 1][i] == 'O') q.push(pair<int, int>(len - 1, i)); } bfsColor(board,len, width); for (int i = 0; i < len; i++) { for (int j = 0; j < width; j++) { if (board[i][j] == 'O') board[i][j] = 'X'; else if (board[i][j] == 'Y') board[i][j] = 'O'; } } }
|