LeetCode-130 - surrounded area

Keywords: Java Algorithm array recursion

Surrounded area

Title Description: give you a matrix board of m x n, which consists of several characters' X 'and' O ', find all areas surrounded by' X ', and fill all' O 'in these areas with' X '.

See LeetCode's official website for an example.

Source: LeetCode
Link: https://leetcode-cn.com/probl...
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Solution 1: recursive method

First, if the array is empty, it does not need to be adjusted and is returned directly.

Then, the processing logic starts from the four sides of the array. If it is connected with it, the characters at the corresponding position will be temporarily reset and updated to 'A'. The specific processing logic is as follows:

  • Start processing from each character of the first row, the last row, the first column and the last column;
  • Judge that if the current coordinate is not within the array range or the value of the current coordinate position is not 'O', skip and do not need to be processed;
  • Judge the value of the current coordinate position. If it is not 'O', it indicates that this character is connected with 'O' on the edge, and temporarily update the value to 'A';
  • Then recursively process the front, back, left and right positions of the current position.

Finally, traverse the array and update the ones marked 'A' in the array to 'O', which are connected to the edges, that is, they are not surrounded by 'X'; Update the array marked 'O' to 'X'. These are disconnected from 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 the array is empty, it does not need to be adjusted and is returned directly
        if (board.length == 0) {
            return;
        }
        row = board.length;
        col = board[0].length;
        // Start processing from the first and last lines
        for (int i = 0; i < row; i++) {
            dfs(board, i, 0);
            dfs(board, i, col - 1);
        }

        // Start processing from the first and last columns
        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) {
        // If the current coordinate is not within the array range or the value of the current coordinate position is not 'O', skip and do not process
        if (x < 0 || x >= row || y < 0 || y >= col || board[x][y] != 'O') {
            return;
        }
        // The value of the current coordinate position is not 'O', indicating that this character is connected with 'O' on the edge. Temporarily update the value to 'A'
        board[x][y] = 'A';
        // Then recursively process the front, back, left and right positions of the current position
        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("-----Before filling-----");
        for (char[] chars : board) {
            for (char c : chars) {
                System.out.print(c);
            }
            System.out.println();
        }
        System.out.println();
        /**
         * Test case, expected output:
         * XXXX
         * XXXX
         * XXXX
         * XOXX
         */
        System.out.println("-----After filling-----");
        solve(board);
        for (char[] chars : board) {
            for (char c : chars) {
                System.out.print(c);
            }
            System.out.println();
        }
    }
}

Delete inferiority from your dictionary. Not everyone can become a great man, but everyone can become a strong person in his heart. Believe in yourself and find your position. You can also have a valuable life.

Posted by fireMind on Fri, 26 Nov 2021 20:00:14 -0800