Daily practice LeetCode: M1706 Where does the ball fall

Posted by yashvant on Fri, 25 Feb 2022 08:40:38 +0100

Write in front

Today, I found a very interesting little girl named "BugNotFoundException", which is very interesting.
Bug s are an indispensable part of a programmer's life. They spend quite a long time with you, and even you have to lose your hair v_v

Looking at the data, college students have done so many questions.
How did I find her? When I met a medium-sized problem, I found that her solution was quite wonderful. I came in and looked at it. It's really frightening for later generations.

I think my math should not be very bad. At least in high school, it has always been the strength of all classes (although I failed in the college entrance examination),
But it took quite a long time to solve this problem.

Finally, there is a bug in the draw function compilation box on the personal home page..
I put in a lot of patterns to achieve the desired effect, but I didn't save them at last... The characters exceeded the limit

Title Description

A box is represented by a two-dimensional grid with the size of m x n. You have n balls. The top and bottom of the box are open.

Each cell in the box has a diagonal baffle, which can guide the ball to the left or right across the two corners of the cell.

Guide the ball to the baffle on the right across the upper left and lower right corners, represented by 1 in the grid.
Guide the ball to the left baffle across the upper right and lower left corners, represented by - 1 in the grid.
Put a ball at the top of each column of the box. Each ball can get stuck in the box or fall out of the bottom. If the ball happens to be stuck in the "V" pattern between the two baffles, or is guided to either side of the box by a baffle, it will get stuck.

Returns an array answer of size n, where answer[i] is the corresponding subscript of the column from the bottom after the ball is placed in column I at the top. If the ball is stuck in the box, it returns - 1.

Example 1:

Input: grid = [[1,1,1,-1,-1],[1,1,1,-1,-1],[-1,-1,-1,1,1],[1,1,1,1,-1],[-1,-1,-1,-1,-1]]
Output:[1,-1,-1,-1,-1]
Explanation: an example is shown in the figure:
b0 The ball began to be placed on column 0 and finally fell out of column 1 at the bottom of the box.
b1 When the ball starts to be placed on column 1, it will get stuck between columns 2, 3 and row 1 "V" In shape.
b2 When the ball starts to be placed on column 2, it will get stuck between columns 2, 3 and row 0 "V" In shape.
b3 When the ball starts to be placed on column 3, it will get stuck between columns 2, 3 and row 0 "V" In shape.
b4 When the ball starts to be placed on column 4, it will get stuck between columns 2, 3 and row 1 "V" In shape.
Example 2:

Input: grid = [[-1]]
Output:[-1]
Explanation: the ball is stuck on the left side of the box.
Example 3:

Input: grid = [[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1],[1,1,1,1,1,1],[-1,-1,-1,-1,-1,-1]]
Output:[0,1,2,3,4,-1]

code

public class M1706 {

    public static int[] findBall(int[][] grid) {
        /*
         * rows: Several lines
         * cols: Several columns
         */
        int rows = grid.length;
        int cols = grid[0].length;
        int[] res = new int[cols];

        for (int c = 0; c < cols; c++) {
            res[c] = dfs(grid, rows, cols, 0, c);
        }

        return res;
    }

    private static int dfs(int[][] grid, int rows, int cols, int row, int col) {

        // Fall into a V-shaped pit
        if ((col + 1 < cols && grid[row][col] == 1 && grid[row][col + 1] == -1) || (col - 1 >= 0 && grid[row][col] == -1 && grid[row][col - 1] == 1)) {
            return -1;
        }
        // Update the current row and col of the small ball
        col += grid[row++][col];
        
        return col < 0 || col >= cols ? -1 : row == rows ? col : dfs(grid, rows, cols, row, col);
    }

    public static void main(String[] args) {
        int[][] gridA = {{1,1,1,-1,-1}, {1,1,1,-1,-1}, {-1,-1,-1,1,1}, {1,1,1,1,-1}, {-1,-1,-1,-1,-1}};
        int[][] gridB = {{-1}};
        int[][] gridC = {{1,1,1,1,1,1}, {-1,-1,-1,-1,-1,-1}, {1,1,1,1,1,1}, {-1,-1,-1,-1,-1,-1}};
        System.out.println("gridA = " + Arrays.toString(findBall(gridA)));
        System.out.println("gridB = " + Arrays.toString(findBall(gridB)));
        System.out.println("gridC = " + Arrays.toString(findBall(gridC)));
    }
}

DFS (Depth First Search): Depth First Search.

Traverse each column, and the position where the ball falls is solved recursively
First, judge whether it falls into the V-shaped pit return -1. Pay attention to ArrayIndexOutOfBoundsException
Update the position of the next step of the small ball column, and the row will increase by 1
If you don't get to the last line, continue to recurse until the last line. If you get to the boundary card, return -1

Attachment: Test drawing

source

Link: https://leetcode-cn.com/problems/where-will-the-ball-fall
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

Topics: Java Algorithm leetcode