runnig

[leetcode] 130 Surrounded Regions (done)

Feb 27th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. /*
  2. http://leetcode.com/onlinejudge#question_130
  3.  
  4. Surrounded Regions
  5. Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
  6.  
  7. A region is captured by flipping all 'O's into 'X's in that surrounded region .
  8.  
  9. For example,
  10. X X X X
  11. X O O X
  12. X X O X
  13. X O X X
  14.  
  15. After running your function, the board should be:
  16. X X X X
  17. X X X X
  18. X X X X
  19. X O X X
  20.  
  21. */
  22.  
  23. class Solution {
  24. public:
  25.     typedef vector<char> row_t;
  26.     typedef vector<row_t> board_t;
  27.    
  28.     void bfs_replace(board_t & board, int r, int c,
  29.         char replacee, char replacer)
  30.     {
  31.         const int R = board.size();        
  32.         const int C = board[0].size();
  33.        
  34.         typedef pair<int, int> intpair;
  35.         deque<intpair> Q;
  36.        
  37.         Q.push_back(make_pair(r,c));
  38.         while(!Q.empty())
  39.         {
  40.             intpair rc = Q.front();
  41.             Q.pop_front();
  42.            
  43.             r = rc.first;
  44.             c = rc.second;
  45.            
  46.             if(r < 0 || r >= R || c < 0 || c >= C) { continue; }
  47.             if(board[r][c] != replacee) { continue; }
  48.             board[r][c] = replacer;
  49.  
  50.             Q.push_back(make_pair(r-1, c));
  51.             Q.push_back(make_pair(r+1, c));
  52.             Q.push_back(make_pair(r, c-1));
  53.             Q.push_back(make_pair(r, c+1));
  54.         }    
  55.     }
  56.     void replace_all(board_t & board, char replacee, char replacer)
  57.     {
  58.         const int R = board.size();        
  59.         const int C = board[0].size();
  60.        
  61.         for(int r = 0; r < R; ++r)
  62.         {
  63.             for(int c = 0; c < C; ++c)
  64.             {
  65.                 if(board[r][c] == replacee)
  66.                 {
  67.                     board[r][c] = replacer;
  68.                 }
  69.             }
  70.         }
  71.     }
  72.    
  73.    
  74.     void solve(board_t &board) {
  75.        
  76.         if(board.empty()) { return; }
  77.        
  78.         const int R = board.size();        
  79.         const int C = board[0].size();
  80.        
  81.         const char replacee = 'O';
  82.         const char mark = 'V';
  83.        
  84.         // mark all 'O' that touch border and their
  85.         // neighbours with mark 'V'
  86.         for(int r = 0; r < R; ++r)
  87.         {
  88.             if(board[r][0] == replacee)
  89.             {
  90.                 bfs_replace(board, r, 0, replacee, mark);
  91.             }
  92.             if(board[r][C-1] == replacee)
  93.             {
  94.                 bfs_replace(board, r, C-1, replacee, mark);
  95.             }
  96.         }        
  97.         for(int c = 0; c < C; ++c)
  98.         {
  99.             if(board[0][c] == replacee)
  100.             {
  101.                 bfs_replace(board, 0, c, replacee, mark);
  102.             }
  103.             if(board[R-1][c] == replacee)
  104.             {
  105.                 bfs_replace(board, R-1, c, replacee, mark);
  106.             }
  107.         }
  108.         // replace all remaining 'O' with 'X'
  109.         replace_all(board, replacee, 'X');
  110.        
  111.         // replace all 'V' back to 'O'
  112.         replace_all(board, mark, replacee);
  113.     }
  114. };
Advertisement
Add Comment
Please, Sign In to add comment