zoukankan      html  css  js  c++  java
  • leetcode-surrounded regions-ZZ

    Problem Statement (link):

    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

    Analysis:
    Rather than recording the 2D positions for any scanned 'O', a trick is to substitute any border 'O's with another character - here in the Code I use 'Y'. And scan the board again to change any rest 'O's to 'X's, and change 'Y's back to 'O's.

    We start searching 'O' from the four borders. I tried DFS first, the OJ gives Runtime error on the 250x250 large board; In the Sol 2 below, I implement BFS instead, and passed all tests.

    The time complexity is O(n^2), as in the worst case, we may need to scan the entire board.

    Code:
    1, DFS

     1 class Solution {
     2 public:
     3     // dfs - Runtime error on large board 250x250
     4     void dfs(vector<vector<char>> &board, int r, int c) {
     5         if (r<0||r>board.size()-1||c<0||c>board[0].size()-1||board[r][c]!='O')
     6             return;
     7         board[r][c]='Y';
     8         dfs(board, r-1, c);
     9         dfs(board, r+1, c);
    10         dfs(board, r, c-1);
    11         dfs(board, r, c+1);
    12     }
    13     void solve(vector<vector<char>> &board) {
    14         if (board.empty() || board.size()<3 || board[0].size()<3)
    15             return;
    16         int r=board.size();
    17         int c=board[0].size();
    18         // dfs from boundary to inside
    19         for (int i=0; i<c; i++) {
    20             if (board[0][i]=='O')
    21                 dfs(board, 0, i);   // first row
    22             if (board[c-1][i]=='O')
    23                 dfs(board, c-1, i); // last row
    24         }
    25         for (int i=0; i<board.size(); i++) {
    26             if (board[i][0]=='O')
    27                 dfs(board, i, 0);   // first col
    28             if (board[i][c-1])
    29                 dfs(board, i, c-1); // last col
    30         }
    31         // scan entire matrix and set values
    32         for (int i=0; i<board.size(); i++) {
    33             for (int j=0; j<board[0].size(); j++) {
    34                 if (board[i][j]=='O')
    35                     board[i][j]='X';
    36                 else if (board[i][j]=='Y')
    37                     board[i][j]='O';
    38             }
    39         }
    40     }
    41 };

    2, BFS

     1 class Solution {
     2 public:
     3     void solve(vector<vector<char>> &board) {
     4         if (board.empty() || board.size()<3 || board[0].size()<3)
     5             return;
     6         int r=board.size();
     7         int c=board[0].size();
     8         // queues to store row and col indices
     9         queue<int> qr;
    10         queue<int> qc;
    11         // start from boundary
    12         for (int i=0; i<c; i++) {
    13             if (board[0][i]=='O') { qr.push(0); qc.push(i); }
    14             if (board[r-1][i]=='O') { qr.push(r-1); qc.push(i); }
    15         }
    16         for (int i=0; i<r; i++) {
    17             if (board[i][0]=='O') { qr.push(i); qc.push(0); }
    18             if (board[i][c-1]=='O') { qr.push(i); qc.push(c-1); }
    19         }
    20         // BFS
    21         while (!qr.empty()) {
    22             int rt=qr.front(); qr.pop();
    23             int ct=qc.front(); qc.pop();
    24             board[rt][ct]='Y';
    25             if (rt-1>=0 && board[rt-1][ct]=='O') { qr.push(rt-1); qc.push(ct); }  //go up
    26             if (rt+1<r && board[rt+1][ct]=='O') { qr.push(rt+1); qc.push(ct); } // go down
    27             if (ct-1>=0 && board[rt][ct-1]=='O') { qr.push(rt); qc.push(ct-1); } // go left
    28             if (ct+1<c && board[rt][ct+1]=='O') { qr.push(rt); qc.push(ct+1); } // go right
    29         }
    30  
    31         // scan entire matrix and set values
    32         for (int i=0; i<board.size(); i++) {
    33             for (int j=0; j<board[0].size(); j++) {
    34                 if (board[i][j]=='O') board[i][j]='X';
    35                 else if (board[i][j]=='Y') board[i][j]='O';
    36             }
    37         }
    38     }
    39 };

    http://justcodings.blogspot.com/2014/07/leetcode-surrounded-regions.html

  • 相关阅读:
    操作系统--进程间同步
    操作系统--进程间通信
    LeetCode-- Unique Binary Search Trees II
    STL源码--序列容器(一)
    操作系统--用户级线程和内核级线程
    非洲孩子
    寻找最大数(三)
    找点
    心急的C小加
    1044 拦截导弹——http://codevs.cn/problem/1044/
  • 原文地址:https://www.cnblogs.com/forcheryl/p/4082004.html
Copyright © 2011-2022 走看看