0
点赞
收藏
分享

微信扫一扫

LeetCode-130. Surrounded Regions [C++][Java]

早安地球 2022-02-28 阅读 68

题目描述

Given an m x n matrix board containing 'X' and 'O'capture all regions that are 4-directionally surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region.

Example 1:

Input: board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
Output: [["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
Explanation: Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.

Example 2:

Input: board = [["X"]]
Output: [["X"]]

Constraints:

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 200
  • board[i][j] is 'X' or 'O'.

 

解题思路

【C++】

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        if (board.empty()) return;
        for (int i = 0; i < board.size(); i++) {
            if (board[i][0] == 'O') dfs(board, i, 0);
            if (board[i][board[0].size()-1] == 'O') dfs(board, i, board[0].size()-1); 
        } 
        for (int i = 0; i < board[0].size(); i++) {
            if (board[0][i] == 'O') dfs(board, 0, i);
            if (board[board.size()-1][i] == 'O') dfs(board, board.size()-1, i);
        }
        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 if (board[i][j] == 'O') board[i][j] = 'X';
            }
        }
    }

    void dfs(vector<vector<char>>& board, int i, int j) {
        if (i < 0 || i > board.size() - 1
            || j < 0 || j > board[0].size() - 1 || board[i][j] != 'O') {return;}
        board[i][j] = '*';
        dfs(board, i + 1, j);
        dfs(board, i - 1, j);
        dfs(board, i, j + 1);
        dfs(board, i, j - 1);
        
    }
};

【Java】

class Solution {
    public void solve(char[][] board) {
        if (board.length == 0 || board[0].length == 0) {return;}
        for (int i = 0; i < board.length; i++) {
            if (board[i][0] == 'O') dfs(board, i, 0);
            if (board[i][board[0].length - 1] == 'O') dfs(board, i, board[0].length - 1); 
        } 
        for (int i = 0; i < board[0].length; i++) {
            if (board[0][i] == 'O') dfs(board, 0, i);
            if (board[board.length - 1][i] == 'O') dfs(board, board.length - 1, i);
        }
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == '*') board[i][j] = 'O';
                else if (board[i][j] == 'O') board[i][j] = 'X';
            }
        }
    }

    void dfs(char[][] board, int i, int j) {
        if (i < 0 || i > board.length - 1
            || j < 0 || j > board[0].length - 1 || board[i][j] != 'O') {return;}
        board[i][j] = '*';
        dfs(board, i + 1, j);
        dfs(board, i - 1, j);
        dfs(board, i, j + 1);
        dfs(board, i, j - 1);
        
    }
}

举报

相关推荐

0 条评论