标签:style blog http io ar color os sp for
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,搜索的过程会过于复杂,如果能够逆向的求解,题目将变得非常容易。思路就是,去搜索不会被X包围的O,将其置为一个特殊字符,然后把剩下的O置为X,再把特殊字符恢复成O即可。
细化下来,去思考怎样的O不会被X包围,在边界的不会被包围,或者与不会被包围的相连,这不就变成了搜索的问题了么,从4个boundary入手,搜索这个矩阵,找到不会被包围的O。
#include<iostream> #include<vector> #include<queue> using namespace std; class Solution { public: void solve(vector<vector<char>> &board) { if(board.empty()||board[0].empty()) return; int row=board.size(); int col=board[0].size(); int i,j; for(i=0; i<row; i++) { bfs(i,0,board); bfs(i,col-1,board); } for(j=0; j<col; j++) { bfs(0,j,board); bfs(row-1,j,board); } for(i=0; i<row; i++) { for(j=0; j<col; j++) { if(board[i][j]==‘O‘) board[i][j]=‘X‘; else if(board[i][j]==‘#‘) board[i][j]=‘O‘; } } } void bfs(int i,int j,vector<vector<char> > &board) { if(board[i][j]!=‘O‘) return; board[i][j]=‘#‘; queue<int> q; int line=i*board[0].size()+j; q.push(line); while(!q.empty()) { line=q.front(); q.pop(); int row=line/board[0].size(); int col=line%board[0].size(); if(row>0&&board[row-1][col]==‘O‘) { board[row-1][col]=‘#‘; q.push((row-1)*board[0].size()+col); } if(row<board.size()-1&&board[row+1][col]==‘O‘) { board[row+1][col]=‘#‘; q.push((row+1)*board[0].size()+col); } if(col>0&&board[row][col-1]==‘O‘) { board[row][col-1]=‘#‘; q.push(row*board[0].size()+col-1); } if(col<board[0].size()-1&&board[row][col+1]==‘O‘) { board[row][col+1]=‘#‘; q.push(row*board[0].size()+col+1); } } } }; int main() { Solution s; vector<vector<char> > vec={{‘X‘,‘X‘,‘X‘,‘X‘},{‘X‘,‘O‘,‘O‘,‘X‘},{‘X‘,‘X‘,‘O‘,‘X‘},{‘X‘,‘O‘,‘X‘,‘X‘}}; //vector<vector<char> > vec={{‘O‘,‘O‘,‘O‘,‘O‘},{‘O‘,‘O‘,‘O‘,‘O‘},{‘O‘,‘O‘,‘O‘,‘O‘},{‘O‘,‘O‘,‘O‘,‘O‘}}; s.solve(vec); for(auto a:vec) { for(auto v:a) cout<<v<<" "; cout<<endl; } }
运行结果:
应用dfs,通不过大集合。
#include<iostream> #include<vector> using namespace std; class Solution { public: void solve(vector<vector<char>> &board) { if(board.empty()||board[0].empty()) return; int row=board.size(); int col=board[0].size(); int i,j; for(i=0;i<row;i++) { dfs(i,0,board); dfs(i,col-1,board); } for(j=0;j<col;j++) { dfs(0,j,board); dfs(row-1,j,board); } for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(board[i][j]==‘O‘) board[i][j]=‘X‘; else if(board[i][j]==‘#‘) board[i][j]=‘O‘; } } } void dfs(int row,int col,vector<vector<char> > &board) { if(row<0||row>=(int)board.size()||col<0||col>=(int)board[0].size()) return; if(board[row][col]!=‘O‘) return; board[row][col]=‘#‘; dfs(row-1,col,board); dfs(row+1,col,board); dfs(row,col-1,board); dfs(row,col+1,board); } }; int main() { Solution s; //vector<vector<char> > vec={{‘X‘,‘X‘,‘X‘,‘X‘},{‘X‘,‘O‘,‘O‘,‘X‘},{‘X‘,‘X‘,‘O‘,‘X‘},{‘X‘,‘O‘,‘X‘,‘X‘}}; vector<vector<char> > vec={{‘O‘,‘O‘,‘O‘,‘O‘},{‘O‘,‘O‘,‘O‘,‘O‘},{‘O‘,‘O‘,‘O‘,‘O‘},{‘O‘,‘O‘,‘O‘,‘O‘}}; s.solve(vec); for(auto a:vec) { for(auto v:a) cout<<v<<" "; cout<<endl; } }
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/wuchanming/p/4131729.html