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
| class Solution { public: struct dequePoint { uint32_t x = 0; uint32_t y = 0; };
bool valid(std::vector<std::vector<char>>& board, uint32_t x, uint32_t y){ if(x >= 0 && x < board[0].size() && y >= 0 && y < board.size()) return board[y][x] == 'O' ? true : false; return false; } bool valid1(std::vector<std::vector<char>>& board, uint32_t x, uint32_t y){ if(x >= 0 && x < board[0].size() && y >= 0 && y < board.size()) return true; return false; }
void bfsHelper(std::vector<std::vector<char>>& board, uint32_t x, uint32_t y){ queue<dequePoint> queue; queue.push(dequePoint{x,y}); board[y][x] = 'x'; while(!queue.empty()){ x = queue.front().x; y = queue.front().y; queue.pop(); if(valid(board, x - 1, y)){ queue.push(dequePoint{x - 1,y}); board[y][x - 1] = 'x'; } if(valid(board, x + 1, y)){ queue.push(dequePoint{x + 1,y}); board[y][x + 1] = 'x'; } if(valid(board, x, y - 1)){ queue.push(dequePoint{x,y - 1}); board[y - 1][x] = 'x'; } if(valid(board, x, y + 1)){ queue.push(dequePoint{x,y + 1}); board[y + 1][x] = 'x'; } } }
void solve(std::vector<std::vector<char>>& board) { for(uint32_t y = 0; y < board.size(); y++){ for(uint32_t x = 0; x < board[0].size(); x++){ if(!valid1(board, x - 1, y) || !valid1(board, x + 1, y) || !valid1(board, x, y - 1) || !valid1(board, x, y + 1)){ if(board[y][x] == 'O'){ bfsHelper(board, x, y); } } } } for(uint32_t y = 0; y < board.size(); y++){ for(uint32_t x = 0; x < board[0].size(); x++){ if(board[y][x] == 'x') board[y][x] = 'O'; else board[y][x] = 'X'; } } } };
|