4
头图

Surrounded area

Title description: Give you an mxn matrix board consisting of several characters'X' and'O'. Find all areas surrounded by'X', and fill all'O's in these areas with'X'.

Please refer to LeetCode official website for example description.

Source: LeetCode
Link: https://leetcode-cn.com/problems/surrounded-regions/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.

Solution one: recursive method

First of all, if the array is empty, no adjustment is required, and it returns directly.

Then, the processing logic is to traverse from the four edges of the array. If it encounters a connection with it, the character at the corresponding position is temporarily reset and updated to'A'. The specific processing logic is as follows:

  • Start processing from each character in the first row, last row, first column, and last column;
  • Judge if the current coordinate is not within the range of the array or the value of the current coordinate position is not'O', skip it without processing;
  • If it is judged that the value of the current coordinate position is not'O', it means that this character is connected with the'O' on the side, and the value is temporarily updated to'A';
  • Then recursively process the four positions before and after the current position.

Finally traverse the array, update the array marked as'A' to'O', these are connected to the edge, that is, not surrounded by'X'; update the array marked as'O' to'X' ', these are not connected to the edges, that is, they are completely surrounded by'X'.

public class LeetCode_130 {
    private static int row, col;

    public static void solve(char[][] board) {
        // 如果数组为空,不需要调整,直接返回
        if (board.length == 0) {
            return;
        }
        row = board.length;
        col = board[0].length;
        // 从第一行和最后一行开始处理
        for (int i = 0; i < row; i++) {
            dfs(board, i, 0);
            dfs(board, i, col - 1);
        }

        // 从第一列和最后一列开始处理
        for (int i = 1; i < col - 1; i++) {
            dfs(board, 0, i);
            dfs(board, row - 1, i);
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (board[i][j] == 'A') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }

    public static void dfs(char[][] board, int x, int y) {
        // 如果当前坐标不在数组范围内或者当前坐标位置的值不是'O',跳过不用处理
        if (x < 0 || x >= row || y < 0 || y >= col || board[x][y] != 'O') {
            return;
        }
        // 当前坐标位置的值不是'O',说明这个字符是和边上的'O'连通的,将值暂时更新为'A'
        board[x][y] = 'A';
        // 然后递归处理当前位置的前后左右四个位置
        dfs(board, x + 1, y);
        dfs(board, x - 1, y);
        dfs(board, x, y + 1);
        dfs(board, x, y - 1);
    }

    public static void main(String[] args) {
        char[][] board = new char[][]{
                {'X', 'X', 'X', 'X'},
                {'X', 'O', 'O', 'X'},
                {'X', 'X', 'O', 'X'},
                {'X', 'O', 'X', 'X'}
        };

        System.out.println("-----填充之前-----");
        for (char[] chars : board) {
            for (char c : chars) {
                System.out.print(c);
            }
            System.out.println();
        }
        System.out.println();
        /**
         * 测试用例,期望输出:
         * XXXX
         * XXXX
         * XXXX
         * XOXX
         */
        System.out.println("-----填充之后-----");
        solve(board);
        for (char[] chars : board) {
            for (char c : chars) {
                System.out.print(c);
            }
            System.out.println();
        }
    }
}
[Daily Message] inferiority from your dictionary. Not everyone can become a great man, but everyone can become a strong person. If you believe in yourself and find your place, you can also have a valuable life.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!