LeetCode130.Surrounded-Regions<被围绕的区域>

题目:被围绕的区域

LeetCode130.Surrounded-Regions<被围绕的区域>_第1张图片

 

思路:

好吧,这题不会。

bfs+递归 dfs+非递归 dfs+并查集 - 被围绕的区域 - 力扣(LeetCode)

LeetCode130.Surrounded-Regions<被围绕的区域>_第2张图片

 将问题转化为与边界O相连的O。有点像岛屿问题了。

LeetCode130.Surrounded-Regions<被围绕的区域>_第3张图片

 

代码是:

//code
 
class Solution {
public:
    void dfs(vector>& board , int i, int j){
        if(i<0 || j<0 || i >=board.size() || j >= board[0].size() || board[i][j]=='X' || board[i][j]=='#'){
            return ; 
        }
        board[i][j]='#';
        dfs(board,i-1,j);
        dfs(board,i+1,j);
        dfs(board,i,j-1);
        dfs(board,i,j+1);
    }
    void solve(vector>& board) {
        //if(board.size()==0) return;
        int row = board.size();
        int col = board[0].size();
        for(int i=0;i

你可能感兴趣的:(深度优先,算法,leetcode,c++)