[Blue Bridge Java Question of the Day] - 14. Where will the ball fall (interesting simulation question)

⭐️Introduction⭐️ _ _

                Hello everyone, I'm a stalker. Today's daily question is very interesting. I wrote it down and shared it with you. It is a very interesting simulation question (it can be called a simulation question hahaha). I hope everyone will try it out, practice together, and stick to daily questions.

⭐️ Highlights ⭐️

2022.2.23——Java daily practice [Blue Bridge Java Question of the Day] - 13. Just reverse the letters
2022.2.18 - Java daily practice [Blue Bridge Java Question of the Day] - 12. The maximum number of points that can be obtained
2022.2.2——Java daily practice [Blue Bridge Java Question of the Day] - 11. Cooking sequence (greedy instant kill difficult questions)
2022.1.25 - Java daily practice [Blue Bridge Java Question of the Day] - 10. Subarray with the smallest length
2022.1.18 - Java daily practice [Blue Bridge Java daily practice] - 9. Palindrome linked list

☀️ 1. Where will the ball fall

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

Each cell in the box has a diagonal baffle that spans the two corners of the cell to direct the ball to the left or right.

The baffle that directs the ball to the right spans the upper left and lower right corners and is represented by a 1 in the grid.
The flap that directs the ball to the left spans the upper right and lower left corners, represented by -1 in the grid.
Place 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 get stuck in the "V" pattern between the two baffles, or is guided by one of the baffles to either side of the case, it will jam.

Returns an array answer of size n, where answer[i] is the index of the column where the ball fell from the bottom after the ball was placed in the ith column at the top, or -1 if the ball is stuck in the box.

Topic link: Where will the ball fall

       Topic Analysis:

       This question may not be difficult, but it is a very good question for beginners to practice simulation and analysis skills. We can first obtain this information through the combined analysis of the title and the figure:

      1. When the ball reaches the boundary, if the flapper then points towards the wall, the ball gets stuck. For example, when the ordinate is 0, when the ball reaches this point and the value is -1 (indicating that the baffle crosses the upper right and upper left corners), it will be stuck. Similarly, when the ordinate is n-1, the value of 1 will be stuck.

      2. At this time, consider the situation where the ball is not on the boundary. When the position value (i, j) of the ball is 1, the baffle points from the upper left corner to the lower right corner, and the ball can only move to the right due to gravity. Note that we found that if (i-1,j) is -1 at this time, the ball will be stuck, and when it is 1, the ball will fall smoothly. Similarly, when (i, j) is -1, we have to consider whether the position of (i+1, j) is -1, otherwise it will be stuck.

      With the above ideas, we can implement the following code:

class Solution {
    int m;
    int n;
    //记录答案
    int[] arr;
    public int[] findBall(int[][] grid) {
         m=grid.length;
         n=grid[0].length;
         arr=new int[n];
        //从下标为0列到n-1列
        for(int i=0;i<n;i++){
            check(i,grid);
        }
        return arr;
    }
    //j表示第几列
    void check(int col,int[][] grid){
        //记录k为小球的出发位置,也就是起始位置。
        int k=col;
        //i为层数,当能走完这个循环,说明球顺利出来
        for(int i=0;i<m;i++){
            //考虑卡在左墙壁的情况
            if(col==0&&grid[i][col]==-1){
                arr[k]=-1;
                return;
            }
            //考虑卡在右墙壁的情况
            if(col==n-1&&grid[i][col]==1){
                arr[k]=-1;
                return;
            }
            //从左往右移动了一格
            if(grid[i][col]==1&&grid[i][col]==grid[i][col+1]){
                col++;
                continue;
            //从右往左移动了一格
            }else if(grid[i][col]==-1&&grid[i][col]==grid[i][col-1]){
                col--;
                continue;
            //从左往右边被卡住的情况
            }else if(grid[i][col]==1&&grid[i][col]!=grid[i][col+1]){
                arr[k]=-1;
                return;
            //从右往左卡住的情况
            }else if(grid[i][col]==-1&&grid[i][col]!=grid[i][col-1]){
                arr[k]=-1;
                return;
            }      
        }
        //能顺利走出for循环,说明小球顺利出来,col的值就是它出来的位置
        arr[k]=col;
    }
}

        I believe that with comments, you can easily understand the above code. If you understand the title, you can try to see the simpler code below (from Sister Sanye).

class Solution {
    int m, n;
    int[][] g;
    public int[] findBall(int[][] grid) {
        g = grid;
        m = g.length; n = g[0].length;
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) ans[i] = getVal(i);
        return ans;
    }
    int getVal(int x) {
        int r = 0, c = x;
        while (r < m) {
            int ne = c + g[r][c];
            if (ne < 0 || ne >= n) return -1;
            if (g[r][c] != g[r][ne]) return -1;
            r++; c = ne;
        }
        return c;
    }
}

        One question per day. Insist on training, dripping through stones!

        If you have a little gain after reading it, Qiuqiu would like to thank you for your support! ! !        

 

Guess you like

Origin blog.csdn.net/m0_57487901/article/details/123106957