LeetCode : Surrounded Regions

504 篇文章 0 订阅
230 篇文章 0 订阅

转自出处

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
[cpp]  view plain copy
  1. class Solution {  
  2. public:  
  3.     struct  myHash  
  4.     {  
  5.         size_t operator()(const pair<int ,int >& key)const{  
  6.             return key.first * 1000 + key.second;  
  7.         }  
  8.     };  
  9.     void clean(vector<vector<bool>> & visit){  
  10.         int row = visit.size();  
  11.         int col = visit[0].size();  
  12.         for(int i = 0; i < row; ++i){  
  13.             for(int j = 0; j < col ;++j){  
  14.                 visit[i][j] = false;  
  15.             }  
  16.         }  
  17.     }  
  18.     void solve(vector<vector<char>> &board) {  
  19.         // Start typing your C/C++ solution below  
  20.         // DO NOT write int main() function  
  21.         //queue<pair<int ,int > > o_pos;//for the O positions  
  22.         queue<pair<int ,int > > q;//for BFS  
  23.         //unordered_set<pair<int ,int> , myHash> unsrd_pos;  
  24.         int neighbour[4][2] = {1,0,  
  25.             0, 1,  
  26.             0, -1,  
  27.             -1, 0  
  28.         };  
  29.         int rowNo = board.size();  
  30.         if(!rowNo)  
  31.             return;  
  32.         int colNo = board[0].size();  
  33.         if(!colNo)  
  34.             return;  
  35.   
  36.         vector<vector<bool>> visit(rowNo, vector<bool>(colNo, false));  
  37.         for(int i = 0; i< rowNo; ++i){  
  38.             for(int j = 0; j < colNo; ++j){  
  39.                 if(board[i][j] == 'O'){  
  40.                     //if(unsrd_pos.find(make_pair(i, j)) != unsrd_pos.end()){  
  41.                     //  continue;  
  42.                     //}  
  43.                     bool isSurrounded = true;  
  44.                     clean(visit);  
  45.                     q.push(make_pair(i, j));  
  46.                     //o_pos.push(make_pair(i,j));  
  47.                     while(!q.empty()){  
  48.                         auto cur = q.front();  
  49.                         q.pop();  
  50.                         int cur_i = cur.first;  
  51.                         int cur_j = cur.second;  
  52.                         visit[cur_i][cur_j] = true;  
  53.                         for(int k = 0; k < 4; ++k){  
  54.                             int next_i = cur_i + neighbour[k][0];  
  55.                             int next_j = cur_j + neighbour[k][1];  
  56.                             if(next_i < 0 || next_i == rowNo || next_j < 0 || next_j == colNo){  
  57.                                 isSurrounded = false;  
  58.                                 break;  
  59.                             }  
  60.                             if(board[next_i][next_j] == 'N'){  
  61.                                 isSurrounded = false;  
  62.                                 break;  
  63.                             }  
  64.                             if(!visit[next_i][next_j] && board[next_i][next_j] == 'O'){  
  65.                                 q.push(make_pair(next_i, next_j));  
  66.                                 //o_pos.push(make_pair(next_i, next_j));  
  67.                             }  
  68.                         }  
  69.                         if(!isSurrounded){  
  70.                             break;  
  71.                         }  
  72.                     }  
  73.                     if(isSurrounded){  
  74.                         /*while(!o_pos.empty()){ 
  75.                             auto cur = o_pos.front(); 
  76.                             o_pos.pop(); 
  77.                             int cur_i = cur.first; 
  78.                             int cur_j = cur.second; 
  79.                             board[cur_i][cur_j] = 'X'; 
  80.                         }*/  
  81.                         board[i][j] = 'X';  
  82.                     }  
  83.                     else{  
  84.                         while(!q.empty()){  
  85.                             q.pop();  
  86.                         }  
  87.                         board[i][j] = 'N';  
  88.                         /* 
  89.                         while(!o_pos.empty()){ 
  90.                             auto cur = o_pos.front(); 
  91.                             o_pos.pop(); 
  92.                             unsrd_pos.insert(cur); 
  93.                         }*/  
  94.                     }  
  95.                 }  
  96.             }  
  97.         }  
  98.         for(int i = 0; i< rowNo; ++i){  
  99.             for(int j = 0; j< colNo; ++j){  
  100.                 if(board[i][j] == 'N'){  
  101.                     board[i][j] = 'O';  
  102.                 }  
  103.             }  
  104.         }  
  105.         return;  
  106.     }  
  107. };   

大概解法就是这样,但是使用hash_set之后超memory,不使用的话TLE,不知道有没有什么好的方法。

今天看到的很好的方法。


[cpp]  view plain copy
  1. class Solution {  
  2.     private:  
  3.         struct Point {  
  4.             int x, y;  
  5.             Point(int _x, int _y):x(_x), y(_y) {}  
  6.         };  
  7.     public:  
  8.         void solve(vector<vector<char> > & board) {  
  9.             const int M = board.size();  
  10.             if (M <= 2) return;  
  11.             const int N = board[0].size();  
  12.             vector<Point> run; // 没被包含的O,判断后修改为D来标记  
  13.             for (int i=0; i<M; ++i)     // 1 边界  
  14.                 for (int j=0; j<N; ++j)   
  15.                     if ((i==0 || i==M-1 || j==0 || j==N-1) && board[i][j]=='O') {   
  16.                         board[i][j] = 'D';  
  17.                         run.push_back(Point(i, j));  
  18.                     }  
  19.   
  20.             const static int PATH[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};  
  21.             while (!run.empty()) {      // 2 out -> insider  
  22.                 Point p = run.back();  
  23.                 run.pop_back();  
  24.                 for (int i=0; i<4; ++i) {  
  25.                     int x = p.x+PATH[i][1];  
  26.                     int y = p.y+PATH[i][0];  
  27.                     if (x<0 || x>=M || y<0 || y>= N || board[x][y]!='O')  
  28.                         continue;  
  29.                     board[x][y] = 'D';  
  30.                     run.push_back(Point(x, y));  
  31.                 }  
  32.             }  
  33.   
  34.             for (int i=0; i<M; ++i)     // 3 检查  
  35.                 for (int j=0; j<N; ++j) {  
  36.                     if (board[i][j]=='X'continue;  
  37.                     board[i][j] = (board[i][j]=='O'?'X':'O');  
  38.                 }  
  39.         }  
  40. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值