Given a 2D board containing 'X'
and 'O'
, capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region .
For example,
X X X X X O O X X X O X X O X X
After running your function, the board should be:
X X X X X X X X X X X X X O X X
- class Solution {
- public:
- struct myHash
- {
- size_t operator()(const pair<int ,int >& key)const{
- return key.first * 1000 + key.second;
- }
- };
- void clean(vector<vector<bool>> & visit){
- int row = visit.size();
- int col = visit[0].size();
- for(int i = 0; i < row; ++i){
- for(int j = 0; j < col ;++j){
- visit[i][j] = false;
- }
- }
- }
- void solve(vector<vector<char>> &board) {
- // Start typing your C/C++ solution below
- // DO NOT write int main() function
- //queue<pair<int ,int > > o_pos;//for the O positions
- queue<pair<int ,int > > q;//for BFS
- //unordered_set<pair<int ,int> , myHash> unsrd_pos;
- int neighbour[4][2] = {1,0,
- 0, 1,
- 0, -1,
- -1, 0
- };
- int rowNo = board.size();
- if(!rowNo)
- return;
- int colNo = board[0].size();
- if(!colNo)
- return;
- vector<vector<bool>> visit(rowNo, vector<bool>(colNo, false));
- for(int i = 0; i< rowNo; ++i){
- for(int j = 0; j < colNo; ++j){
- if(board[i][j] == 'O'){
- //if(unsrd_pos.find(make_pair(i, j)) != unsrd_pos.end()){
- // continue;
- //}
- bool isSurrounded = true;
- clean(visit);
- q.push(make_pair(i, j));
- //o_pos.push(make_pair(i,j));
- while(!q.empty()){
- auto cur = q.front();
- q.pop();
- int cur_i = cur.first;
- int cur_j = cur.second;
- visit[cur_i][cur_j] = true;
- for(int k = 0; k < 4; ++k){
- int next_i = cur_i + neighbour[k][0];
- int next_j = cur_j + neighbour[k][1];
- if(next_i < 0 || next_i == rowNo || next_j < 0 || next_j == colNo){
- isSurrounded = false;
- break;
- }
- if(board[next_i][next_j] == 'N'){
- isSurrounded = false;
- break;
- }
- if(!visit[next_i][next_j] && board[next_i][next_j] == 'O'){
- q.push(make_pair(next_i, next_j));
- //o_pos.push(make_pair(next_i, next_j));
- }
- }
- if(!isSurrounded){
- break;
- }
- }
- if(isSurrounded){
- /*while(!o_pos.empty()){
- auto cur = o_pos.front();
- o_pos.pop();
- int cur_i = cur.first;
- int cur_j = cur.second;
- board[cur_i][cur_j] = 'X';
- }*/
- board[i][j] = 'X';
- }
- else{
- while(!q.empty()){
- q.pop();
- }
- board[i][j] = 'N';
- /*
- while(!o_pos.empty()){
- auto cur = o_pos.front();
- o_pos.pop();
- unsrd_pos.insert(cur);
- }*/
- }
- }
- }
- }
- for(int i = 0; i< rowNo; ++i){
- for(int j = 0; j< colNo; ++j){
- if(board[i][j] == 'N'){
- board[i][j] = 'O';
- }
- }
- }
- return;
- }
- };
大概解法就是这样,但是使用hash_set之后超memory,不使用的话TLE,不知道有没有什么好的方法。
今天看到的很好的方法。
- class Solution {
- private:
- struct Point {
- int x, y;
- Point(int _x, int _y):x(_x), y(_y) {}
- };
- public:
- void solve(vector<vector<char> > & board) {
- const int M = board.size();
- if (M <= 2) return;
- const int N = board[0].size();
- vector<Point> run; // 没被包含的O,判断后修改为D来标记
- for (int i=0; i<M; ++i) // 1 边界
- for (int j=0; j<N; ++j)
- if ((i==0 || i==M-1 || j==0 || j==N-1) && board[i][j]=='O') {
- board[i][j] = 'D';
- run.push_back(Point(i, j));
- }
- const static int PATH[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
- while (!run.empty()) { // 2 out -> insider
- Point p = run.back();
- run.pop_back();
- for (int i=0; i<4; ++i) {
- int x = p.x+PATH[i][1];
- int y = p.y+PATH[i][0];
- if (x<0 || x>=M || y<0 || y>= N || board[x][y]!='O')
- continue;
- board[x][y] = 'D';
- run.push_back(Point(x, y));
- }
- }
- for (int i=0; i<M; ++i) // 3 检查
- for (int j=0; j<N; ++j) {
- if (board[i][j]=='X') continue;
- board[i][j] = (board[i][j]=='O'?'X':'O');
- }
- }
- };