题目:
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
题目来源:https://oj.leetcode.com/problems/surrounded-regions/
解题思路:深搜,从边界开始不断从上下左右寻找O,如果找到就标记,这些标记的O是不需要编程X的。广搜,从边界上的一个点开始进行广搜,和深搜做法一样。
深搜的代码提交后有一个数据会出现RE,不知道怎么回事,用往上其他人的程序也是类似情况,同样的数据,用广搜就不会出现这样的情况。
深搜代码:#include<iostream>
#include<vector>
#include<queue>
using namespace std;
void dfs(vector<vector<char> >&board,int row,int col,vector<vector<bool> > &flags)
{
if(row<0 || row>board.size()-1 || col<0 || col>board[0].size()-1)//超出边界
return ;
if(flags[row][col]==true)//说明已经访问过了
return ;
if(board[row][col]=='X')//说明此方向不存在o,
return ;
else
{
flags[row][col]=true;
dfs(board,row-1,col,flags);//搜索上
dfs(board,row+1,col,flags);//下
dfs(board,row,col-1,flags);//左
dfs(board,row,col+1,flags);//右
}
}
void solve(vector<vector<char>> &board)
{
if(board.empty())
return ;
vector<vector<bool> > flags(board.size(),vector<bool>(board[0].size(),false));
flags[0][0]=board[0][0]=='O';//左上角
flags[0][board[0].size()-1]=board[0][board[0].size()-1]=='O';//右上角角
flags[board.size()-1][0]=board[board.size()-1][0]=='O';//左下角
flags[board.size()-1][board[0].size()-1]=board[board.size()-1][board[0].size()-1]=='O';//右上角
for(int i=1;i<board[0].size()-1;i++)//从第一行开始搜,首尾不用搜,因为四个角的值不会改变
{
dfs(board,0,i,flags);
dfs(board,board.size()-1,i,flags);
}
for(int i=1;i<board.size()-1;i++)
{
dfs(board,i,0,flags);
dfs(board,i,board[0].size()-1,flags);
}
for(int i=0;i<board.size();i++)
{
for(int j=0;j<board[0].size();j++)
{
if(flags[i][j]==false)
board[i][j]='X';
}
}
}
int main()
{
char *C[3]={"OOO","OOO","OOO"};
vector<vector<char> > board;
for(int i=0;i<3;i++)
board.push_back(vector<char>(C[i],C[i]+3));
solve(board);
system("pause");
return 0;
}
广搜代码:
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
void visite(vector<vector<char> >&board,int row,int col,queue<int> &q)
{
if(row<0 || row>board.size()-1 || col<0 || col>board[0].size()-1 || board[row][col]!='O')
return ;
q.push(row*board[0].size()+col);
board[row][col]='+';
}
void bfs(vector<vector<char> >&board,int row,int col)
{
queue<int> q;
visite(board,row,col,q);
while(!q.empty())
{
int temp=q.front();
q.pop();
int r=temp/board[0].size();
int c=temp%board[0].size();
visite(board,r-1,c,q);
visite(board,r+1,c,q);
visite(board,r,c-1,q);
visite(board,r,c+1,q);
}
}
void solve(vector<vector<char>> &board)
{
if(board.empty())
return ;
for(int i=0;i<board[0].size();i++)//从第一行开始搜,首尾不用搜,因为四个角的值不会改变
{
bfs(board,0,i);
bfs(board,board.size()-1,i);
}
for(int i=1;i<board.size()-1;i++)
{
bfs(board,i,0);
bfs(board,i,board[0].size()-1);
}
for(int i=0;i<board.size();i++)
{
for(int j=0;j<board[0].size();j++)
{
if(board[i][j]=='+')
board[i][j]='O';
else
board[i][j]='X';
}
}
}
int main()
{
char *C[3]={"OOO","OOO","OOO"};
vector<vector<char> > board;
for(int i=0;i<3;i++)
board.push_back(vector<char>(C[i],C[i]+3));
solve(board);
system("pause");
return 0;
}