其他分享
首页 > 其他分享> > box rotate

box rotate

作者:互联网

1861. Rotating the Box Medium You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the box is one of the following:

The box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until it lands on an obstacle, another stone, or the bottom of the box. Gravity does not affect the obstacles' positions, and the inertia from the box's rotation does not affect the stones' horizontal positions.

It is guaranteed that each stone in box rests on an obstacle, another stone, or the bottom of the box.

Return an n x m matrix representing the box after the rotation described above.

Example 1:

Input: box = [["#",".","#"]]
Output: [["."],
         ["#"],
         ["#"]]

Example 2:

Input: box = [["#",".","*","."],
              ["#","#","*","."]]
Output: [["#","."],
         ["#","#"],
         ["*","*"],
         [".","."]]

Example 3:

Input: box = [["#","#","*",".","*","."],
              ["#","#","#","*",".","."],
              ["#","#","#",".","#","."]]
Output: [[".","#","#"],
         [".","#","#"],
         ["#","#","*"],
         ["#","*","."],
         ["#",".","*"],
         ["#",".","."]] 

Constraints:

class Solution {
    public char[][] rotateTheBox(char[][] box) {
        for(int i=0;i<box.length;i++){
            rotateRow(box[i]);
        }
        //向右翻转矩阵
        char[][] result = new char[box[0].length][box.length];
        for(int i=0;i<box.length;i++){
            for(int j=0;j<box[0].length;j++){
                //之前的第一行为现在的最后一列,之前的第一列为现在的第一行
                //因此: 列转行 j->j   行转列  i->len-i-1
                result[j][box.length-1-i]=box[i][j];
            }
        }
        return result;
    }
    private void rotateRow(char[] row){
        int base = row.length-1;
        for(int i=row.length-1;i>=0;i--){
            //如果遇到障碍物
            if(row[i]=='*') {
                //base向上移动 清空,直至碰到障碍物
                while(base>i ) row[base--]='.';
                //将新base置为障碍物之上的第一个位置
                base=i-1;
            }
            //如果是石头,往base处进行填充
            else if(row[i]=='#') row[base--]=row[i];
        }
        while( base>=0 ) row[base--]='.';
    }
}
723. Candy Crush Medium

This question is about implementing a basic elimination algorithm for Candy Crush.

Given an m x n integer array board representing the grid of candy where board[i][j] represents the type of candy. A value of board[i][j] == 0 represents that the cell is empty.

The given board represents the state of the game following the player's move. Now, you need to restore the board to a stable state by crushing candies according to the following rules:

You need to perform the above rules until the board becomes stable, then return the stable board.

Example 1:

Input: board = [[110,5,112,113,114],[210,211,5,213,214],[310,311,3,313,314],[410,411,412,5,414],[5,1,512,3,3],[610,4,1,613,614],[710,1,2,713,714],[810,1,2,1,1],[1,1,2,2,2],[4,1,4,4,1014]]
Output: [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[110,0,0,0,114],[210,0,0,0,214],[310,0,0,113,314],[410,0,0,213,414],[610,211,112,313,614],[710,311,412,613,714],[810,411,512,713,1014]]

Example 2:

Input: board = [[1,3,5,5,2],[3,4,3,3,1],[3,2,4,5,2],[2,4,4,5,5],[1,4,4,1,1]]
Output: [[1,3,0,0,0],[3,4,0,5,2],[3,2,0,3,1],[2,4,0,5,2],[1,4,3,1,1]]

Constraints:

class Solution {
    public int[][] candyCrush(int[][] board) {
        //while loop
        while(true){
            if(crush(board)) 
                falldown(board);
            else break;
        }
        return board;
    }
    
    private boolean crush(int[][] board){
        boolean flag=false;
        int m=board.length,n=board[0].length;
        //判定横向相连的
        for(int i=0;i<m;i++){
            for(int j=0;j<n-2;j++){
                int curr = Math.abs(board[i][j]);
                if(curr==Math.abs(board[i][j+1]) && curr==Math.abs(board[i][j+2]) && curr!=0){
                    board[i][j]=board[i][j+1]=board[i][j+2]=-curr;
                    flag=true;
                }
            }
        }
        //判定纵向相连的
        for(int i=0;i<m-2;i++){
            for(int j=0;j<n;j++){
                int curr = Math.abs(board[i][j]);
                if(curr==Math.abs(board[i+1][j]) && curr==Math.abs(board[i+2][j]) && curr!=0){
                    board[i][j]=board[i+1][j]=board[i+2][j]=-curr;
                    flag=true;
                }
            }
        }      
        return flag;
    }
    private void falldown(int[][] board){
        int row=board.length,col=board[0].length;
        for(int i=0;i<col;i++){
            //双指针,base指向第一个不为0的数,j指向当前遍历元素
            int base=row-1;
            for(int j=row-1;j>=0;j--){
                if(board[j][i]>0) board[base--][i]=board[j][i];
            }
            //当j指针到达最上面以后,base以上的位置都可以置0了
            while(base>=0){
                board[base--][i]=0;
            }
        }
    }
}

 

标签:box,rotate,candies,length,base,board,row
来源: https://www.cnblogs.com/cynrjy/p/15926327.html