题目:
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题目分析:
题目的意思是 》》
给定一个包含'x'和'O'的二维板,捕捉所有被'x'包围的区域。一个区域是在被包围的区域中翻转所有的O成是 X。
例如,
X X X X
X O O X
X X O X
X O X X
运行完函数之后 ,二维板变成是:
X X X X
X X X X
X X X X
X O X X
解决思路:
在上面的题目中,我们知道元素为 O的二维位置,只要可以直通到边界,那么就不需要改变,所以我们可以直接从边界的位置来遍历,找到所有的与边界O相同的位置,剩下的就是被包围的了。
在这里向大家提供两个解决遍历方法:
1、DFS(深度遍历)
#include<stack>
class Solution {
public:
//将所有与边界相同的点都先设置成是#
void DFS(int i,int j,vector<vector<char>> &board,int rows,int cols)
{
board[i][j] = '#';
//向上遍历
if(i>0&&board[i-1][j] == 'O')
{
DFS(i-1,j,board,rows,cols);
}
//向右遍历
if(j<cols-1&&board[i][j+1] == 'O' )
{
DFS(i,j+1,board,rows,cols);
}
//向下遍历
if(i<cols-1&&board[i+1][j] == 'O' )
{
DFS(i+1,j,board,rows,cols);
}
//向左遍历
if(j>0 &&board[i][j-1] == 'O' )
{
DFS(i,j+1,board,rows,cols);
}
}
void solve(vector<vector<char>> &board) {
//表示的是 行的长度
int rows = board.size();
if(rows == 0)
return ;
//表示的列的长度
int cols = board[0].size();
//要是这儿二维数组只有两行或者是 两列的的话,就不需要来管了。
if(rows <= 2||cols<=2)
return ;
//遍历边界的点、
//遍历第一行和最后一行;
for(int i = 0 ;i < cols;++i)
{
if(board[0][i] == 'O')
{
DFS(0,i,board,rows,cols);
}
if(board[rows-1][i] == 'O')
{
DFS(rows-1,i,board,rows,cols);
}
}
//遍历第一列和最后一列
for(int i = 0 ;i < rows;++i)
{
if(board[i][0] == 'O')
{
DFS(i,0,board,rows,cols);
}
if(board[i][cols-1] == 'O')
{
DFS(i,cols-1,board,rows,cols);
}
}
for(int i =0 ;i < rows;++i)
{
for(int j = 0 ;j <cols;++j)
{
if(board[i][j] == '#')
board[i][j] = 'O';
else if(board[i][j] == 'O')
board[i][j] = 'X';
}
}
}
};
2、BFS(广度遍历)
#include<queue>
class Solution {
public:
//将所有与边界相同的点都先设置成是#
void BFS(int i,int j,vector<vector<char>> &board,int rows,int cols)
{
queue<pair<int,int>> dq;
dq.push(make_pair(i,j));
while(!dq.empty())
{
int ii = dq.front().first;
int jj = dq.front().second;
board[ii][jj] = '#';
//向上遍历
if(ii>0&&board[ii-1][jj] == 'O')
{
dq.push(make_pair(ii-1,j));
}
//向右遍历
if(jj<cols-1&&board[ii][jj+1] == 'O' )
{
dq.push(make_pair(ii,jj+1));
}
//向下遍历
if(ii<cols-1&&board[ii+1][jj] == 'O' )
{
dq.push(make_pair(ii+1,jj));
}
//向左遍历
if(jj>0 &&board[ii][jj-1] == 'O' )
{
dq.push(make_pair(ii,jj-1));
}
dq.pop();
}
}
void solve(vector<vector<char>> &board) {
//表示的是 行的长度
int rows = board.size();
if(rows == 0)
return ;
//表示的列的长度
int cols = board[0].size();
//要是这儿二维数组只有两行或者是 两列的的话,就不需要来管了
if(rows <= 2||cols<=2)
return ;
//遍历边界的点、
//遍历第一行和最后一行;
for(int i = 0 ;i < cols;++i)
{
if(board[0][i] == 'O')
{
BFS(0,i,board,rows,cols);
}
if(board[rows-1][i] == 'O')
{
BFS(rows-1,i,board,rows,cols);
}
}
//遍历第一列和最后一列
for(int i = 0 ;i < rows;++i)
{
if(board[i][0] == 'O')
{
BFS(i,0,board,rows,cols);
}
if(board[i][cols-1] == 'O')
{
BFS(i,cols-1,board,rows,cols);
}
}
for(int i =0 ;i < rows;++i)
{
for(int j = 0 ;j <cols;++j)
{
if(board[i][j] == '#')
board[i][j] = 'O';
else if(board[i][j] == 'O')
board[i][j] = 'X';
}
}
}
};