Surrounded Regions

2014.2.27 00:59

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

Solution:

  This problem is a test on Flood Fill Algorithm. You can do it with DFS or BFS, but the latter will result in TLE.

  Why is BFS so slow? Because the queue operation is not strictly O(1), but rather ammortized to O(1). The bad case for some of the queue operations will be O(n). While using BFS you don't have to worry about call stack overflow, but rather the speed of a queue.

  Total time complexity is O(n^2). Space complexity is O(n^2), mainly from parameters in recursive function calls.

Accepted code:

 1 // 1CE, 3TLE, 1AC, DFS accepted but BFS failed?
 2 #include <queue>
 3 #include <vector>
 4 using namespace std;
 5 
 6 class Solution {
 7 public:
 8     void solve(vector<vector<char> > &board) {
 9         // Should n or m be smaller than 3, there'll be no captured region.
10         n = (int)board.size();
11         if (n < 3) {
12             return;
13         }
14         
15         m = (int)board[0].size();
16         if (m < 3) {
17             return;
18         }
19         
20         int i, j;
21         
22         // if an 'O' is on the border, all of its connected 'O's are not captured.
23         // so we scan the border and mark those 'O's as free.
24         
25         // the top row
26         for (j = 0; j < m; ++j) {
27             if (board[0][j] == 'O') {
28                 check_region(board, 0, j);
29             }
30         }
31         
32         // the bottom row
33         for (j = 0; j < m; ++j) {
34             if (board[n - 1][j] == 'O') {
35                 check_region(board, n - 1, j);
36             }
37         }
38         
39         // the left column
40         for (i = 1; i < n - 1; ++i) {
41             if (board[i][0] == 'O') {
42                 check_region(board, i, 0);
43             }
44         }
45         
46         // the right column
47         for (i = 1; i < n - 1; ++i) {
48             if (board[i][m - 1] == 'O') {
49                 check_region(board, i, m - 1);
50             }
51         }
52         
53         // other unchecked 'O's are all captured
54         for (i = 0; i < n; ++i) {
55             for (j = 0; j < m; ++j) {
56                 if (board[i][j] == '#') {
57                     // free 'O's
58                     board[i][j] = 'O';
59                 } else if (board[i][j] == 'O') {
60                     // captured 'O's
61                     board[i][j] = 'X';
62                 }
63             }
64         }
65     }
66 private:
67     int n, m;
68     
69     void check_region(vector<vector<char> > &board, int startx, int starty) {
70         if (startx < 0 || startx > n - 1 || starty < 0 || starty > m - 1) {
71             return;
72         }
73         if (board[startx][starty] == 'O') {
74             board[startx][starty] = '#';
75             check_region(board, startx - 1, starty);
76             check_region(board, startx + 1, starty);
77             check_region(board, startx, starty - 1);
78             check_region(board, startx, starty + 1);
79         }
80     }
81 };

 

 posted on 2014-02-27 01:27  zhuli19901106  阅读(412)  评论(0编辑  收藏  举报