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
| class Solution { public: void dfs(vector<vector<char>>& grid, int x, int y) { grid[x][y] = 'A'; pair<int, int> neigbors[4]{ pair<int, int>(x, y + 1), pair<int, int>(x + 1, y), pair<int, int>(x, y - 1), pair<int, int>(x - 1, y)}; for (auto item : neigbors) { if (item.first < 0 || item.first >= grid.size() || item.second < 0 || item.second >= grid[0].size()) continue; if (grid[item.first][item.second] == 'A' || grid[item.first][item.second] == 'X') continue; dfs(grid, item.first, item.second); } } void solve(vector<vector<char>>& board) { cout << "0step" << endl; for(int i = 0; i < board.size(); i++) { if(board[i][0] == 'O') dfs(board,i,0); if(board[i][board[0].size()-1] == 'O') dfs(board,i, board[0].size()-1); } cout << "1step" << endl; for(int i = 0; i < board[0].size(); i++) { if(board[0][i] == 'O') dfs(board,0,i); if(board[board.size()-1][i] == 'O') dfs(board,board.size()-1,i); } cout << "2step" << endl; for(int i = 0; i < board.size(); i++) { for(int j = 0; j < board[0].size(); j++) { if(board[i][j] == 'O') board[i][j] = 'X'; if(board[i][j] == 'A') board[i][j] = 'O'; } } cout << "3step" << endl; } };
|