码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Surrounded Regions

时间:2014-06-30 15:03:43      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   cti   for   leetcode   

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

思路是从外围是’O‘的开始深度往里走,这时候里面的‘O‘就有两种命运,一种是跟外围的’O‘是联通的,那么这些‘O‘就可以存活,剩下的孤立的’O‘就没办法了,就只能被‘X‘了,为了区分这两种’O‘,我们把联通 的‘O‘改为另外一种统一而独立的字符,便于后期做恢复。这就是三步走(或两步)。

1)首先从外围的‘O‘处深度搜索,见到链接的’O‘就把他们都改为其他标识。

2)将剩余的孤立的’O‘改为‘X‘,同时,将遇到标识符改为’O‘。

简单吧,简单的一塌糊涂,但是这种思路太精髓了。

 深度周游DFS

 1 class Solution {
 2     public:
 3         void dfs(vector<vector<char> > &board, int i, int j)
 4         {
 5             size_t row = board.size();
 6             size_t col = board[0].size();
 7 
 8             if(i < 0 || i > row-1 || j < 0 || j > col-1)
 9                 return;
10             if(board[i][j] != O)
11                 return;
12             board[i][j] = *;//tag, in order to reverse back
13             dfs(board, i, j-1);
14             dfs(board, i, j+1);
15             dfs(board, i-1, j);
16             dfs(board, i+1, j);
17         }
18 
19         void solve(vector<vector<char> > &board)
20         {
21             size_t row = board.size();
22             size_t col = board[0].size();
23 
24             // dfs traverse form the up and down
25             for(int j = 0; j < col; j++)
26             {   
27                 dfs(board, 0 ,j);
28                 dfs(board,row-1, j); 
29             }   
30 
31             // dfs traverse form the left and right 
32             for(int i = 0; i < row; i++)
33             {   
34                 dfs(board, i, 0); 
35                 dfs(board, i, col-1);
36             }
37 
38             for(int i = 0; i < row; i++)
39             {
40                 for(int j = 0; j < col; j++)
41                 {
42                     if(board[i][j] == O)
43                         board[i][j] = X;
44                     else if(board[i][j] == *)
45                         board[i][j] = O;
46                 }
47             }
48 
49 
50         }
51 };

 广度周游BFS

 1 class Solution {
 2             queue<pair<int, int> > m_que;
 3     public:
 4         void dfs(vector<vector<char> > &board, int i, int j)
 5         {
 6             size_t row = board.size();
 7             size_t col = board[0].size();
 8 
 9             if(i < 0 || i > row-1 || j < 0 || j > col-1)
10                 return;
11             if(board[i][j] != O)
12                 return;
13             board[i][j] = *;//tag, in order to reverse back
14             dfs(board, i, j-1);
15             dfs(board, i, j+1);
16             dfs(board, i-1, j);
17             dfs(board, i+1, j);
18         }
19 
20         void fill(vector<vector<char> > &board, int x, int y){
21             size_t row = board.size();
22             size_t col = board[0].size();
23 
24             if(x<0 || x>=row || y<0 || y>=col || board[x][y]!=O)
25                 return;
26 
27             pair<int, int> p ;
28             p.first = x;
29             p.second = y;
30             m_que.push(p);
31 
32             board[x][y]=*;
33         }
34 
35 
36         void bfs(vector<vector<char> > &board, int i, int j)
37         {
38             fill(board, i, j);
39 
40             while(!m_que.empty())
41             {
42                 pair<int, int> p = m_que.front() ;
43                 m_que.pop();
44 
45                 i = p.first;
46                 j = p.second;
47 
48                 fill(board, i, j-1);
49                 fill(board, i, j+1);
50                 fill(board, i-1, j);
51                 fill(board, i+1, j);
52             }
53 
54         }
55 
56         void solve(vector<vector<char> > &board)
57         {
58             if (board.empty()) return;
59             size_t row = board.size();
60             size_t col = board[0].size();
61 
62             // dfs traverse form the up and down
63             for(int j = 0; j < col; j++)
64             {
65 #if 0                
66                 dfs(board, 0 ,j);
67                 dfs(board,row-1, j);
68 #endif
69                 bfs(board, 0 ,j);
70                 bfs(board,row-1, j);
71 
72             }
73 
74             // dfs traverse form the left and right 
75             for(int i = 0; i < row; i++)
76             {
77 #if 0
78                 dfs(board, i, 0);
79                 dfs(board, i, col-1);
80 #endif
81                 bfs(board, i, 0);
82                 bfs(board, i, col-1);
83 
84             }
85 
86             for(int i = 0; i < row; i++)
87             {
88                 for(int j = 0; j < col; j++)
89                 {
90                     if(board[i][j] == O)
91                         board[i][j] = X;
92                     else if(board[i][j] == *)
93                         board[i][j] = O;
94                 }
95             }
96 
97 
98         }
99 };

 

[LeetCode] Surrounded Regions,布布扣,bubuko.com

[LeetCode] Surrounded Regions

标签:style   blog   color   cti   for   leetcode   

原文地址:http://www.cnblogs.com/diegodu/p/3816283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!