zoukankan      html  css  js  c++  java
  • 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions

     1 Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
     2 
     3 A region is captured by flipping all 'O's into 'X's in that surrounded region.
     4 
     5 For example,
     6 X X X X
     7 X O O X
     8 X X O X
     9 X O X X
    10 After running your function, the board should be:
    11 
    12 X X X X
    13 X X X X
    14 X X X X
    15 X O X X
     

     

     1 /*
     2 60 / 60 test cases passed.
     3 Status: Accepted
     4 Runtime: 13 ms
     5 */
     6 
     7 class Solution {
     8 public:
     9     void dfs(vector<vector<char>>& board, int x, int y, int r, int c, vector<vector<bool>> &visited)
    10     {
    11         if(!visited[x][y] && board[x][y] == 'O')
    12         {
    13             visited[x][y] = true;
    14             board[x][y] = '#';
    15             if((x > 0) && (!visited[x-1][y]) && (board[x-1][y] == 'O')) dfs(board, x-1, y, r, c, visited); // top
    16             if((x+1 < r) && (!visited[x+1][y]) && (board[x+1][y] == 'O')) dfs(board, x+1, y, r, c, visited); // bottom
    17             if((y > 1) && (!visited[x][y-1]) && (board[x][y-1] == 'O')) dfs(board, x, y-1, r, c, visited); // left   ??? why "y > 0" can't pass the last test case.
    18             if((y+1 < c) && (!visited[x][y+1]) && (board[x][y+1] == 'O')) dfs(board, x, y+1, r, c, visited); // right
    19         }
    20         return;
    21     }
    22 
    23     void solve(vector<vector<char>>& board)
    24     {
    25         size_t rlen = board.size();
    26         if(0 == rlen) return;
    27         size_t clen = board[0].size();
    28 
    29         cout << board.size() << " " << board[0].size() << endl;
    30         vector<vector<bool>> visited(rlen, vector<bool>(clen, false));
    31 
    32         for(int i=0; i<clen; i++) dfs(board, 0, i, rlen, clen, visited); // top
    33         for(int j=0; j<clen; j++) dfs(board, rlen-1, j, rlen, clen, visited); // bottom
    34         for(int m=0; m<rlen; m++) dfs(board, m, 0, rlen, clen, visited); // left
    35         for(int n=0; n<rlen; n++) dfs(board, n, clen-1, rlen, clen, visited); // right
    36 
    37         for(int i = 0; i < rlen; i++)
    38             for(int j = 0; j < clen; j++)
    39             {
    40                 if(board[i][j] == 'O')
    41                     board[i][j] = 'X';
    42                 if(board[i][j] == '#')
    43                     board[i][j] = 'O';
    44             }         
    45     }
    46 
    47 };
    View Code
     1 /*
     2 Concise 12ms C++ DFS solution
     3 https://discuss.leetcode.com/topic/45119/concise-12ms-c-dfs-solution
     4 */
     5 class Solution { //by Kenigma 
     6 public:
     7     void solve(vector<vector<char>>& board) {
     8         if (board.empty()) return;
     9         int row = board.size(), col = board[0].size();
    10         for (int i = 0; i < row; ++i) {
    11             check(board, i, 0);             // first column
    12             check(board, i, col - 1);       // last column
    13         }
    14         for (int j = 1; j < col - 1; ++j) {
    15             check(board, 0, j);             // first row
    16             check(board, row - 1, j);       // last row
    17         }
    18         for (int i = 0; i < row; ++i)
    19             for (int j = 0; j < col; ++j)
    20                 if (board[i][j] == 'O') board[i][j] = 'X';
    21                 else if (board[i][j] == '1') board[i][j] = 'O';
    22     }
    23     
    24     void check(vector<vector<char>>& board, int i, int j) {
    25         if (board[i][j] == 'O') {
    26             board[i][j] = '1';
    27             if (i > 1) check(board, i - 1, j);
    28             if (j > 1) check(board, i, j - 1);
    29             if (i + 1 < board.size()) check(board, i + 1, j);
    30             if (j + 1 < board[0].size()) check(board, i, j + 1);
    31         }
    32     }
    33 };
    View Code
  • 相关阅读:
    shell编程:for循环结构
    shell编程:实现shell字符串连接功能
    jq 出现 $.cookie is not a function
    jq页面换肤效果
    js ==与===区别(两个等号与三个等号)
    JS与Jquery的事件委托机制
    jq选项卡切换功能
    licecap图片区域问题
    jquery网页定位导航特效
    ps-手捧城堡滴水云雾图
  • 原文地址:https://www.cnblogs.com/guxuanqing/p/6557996.html
Copyright © 2011-2022 走看看