美文网首页
Candy Crush

Candy Crush

作者: BLUE_fdf9 | 来源:发表于2018-11-14 01:56 被阅读0次

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

    Given a 2D integer array board representing the grid of candy, different positive integers board[i][j] represent different types of candies. A value of board[i][j] = 0 represents that the cell at position (i, j) 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:

    If three or more candies of the same type are adjacent vertically or horizontally, "crush" them all at the same time - these positions become empty.
    After crushing all candies simultaneously, if an empty space on the board has candies on top of itself, then these candies will drop until they hit a candy or bottom at the same time. (No new candies will drop outside the top boundary.)
    After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.
    If there does not exist more candies that can be crushed (ie. the board is stable), then return the current board.
    You need to perform the above rules until the board becomes stable, then return the current board.

    答案

    class Solution {
        private void add_vert(int[][] board, int i, int j, List<int[]> list) {
            int cnt = 1, m = board.length, curr_i = i + 1;
            while(curr_i < m && board[curr_i][j] == board[i][j]) {
                cnt++;
                curr_i++;
            }
            if(cnt >= 3) {
                int[] indices = new int[]{i, j, i + cnt - 1, j};
                list.add(indices);
            }
        }
    
        private void add_horz(int[][] board, int i, int j, List<int[]> list) {
            int cnt = 1, n = board[0].length, curr_j = j + 1;
            while(curr_j < n && board[i][curr_j] == board[i][j]) {
                cnt++;
                curr_j++;
            }
            if(cnt >= 3) {
                int[] indices = new int[]{i, j, i, j + cnt - 1};
                list.add(indices);
            }
        }
    
        private void crush(int[][] board, int[] seq) {
            int start_i = seq[0], start_j = seq[1], end_i = seq[2], end_j = seq[3];
            if(start_i == end_i) {
                while(start_j <= end_j) {
                    board[start_i][start_j++] = 0;
                }
            }
            else {
                while(start_i <= end_i) {
                    board[start_i++][start_j] = 0;
                }
            }
        }
    
        private void swap(int[][] board, int i, int j, int i2, int j2) {
            int t = board[i][j];
            board[i][j] = board[i2][j2];
            board[i2][j2] = t;
        }
    
        public int[][] candyCrush(int[][] board) {
            List<int[]> list = new ArrayList<>();
            int m = board.length, n = board[0].length;
    
            // Find candies to crush
            for(int i = 0; i < m; i++) {
                for(int j = 0; j < n; j++) {
                    if(board[i][j] == 0) continue;
                    if(i == 0 || board[i][j] != board[i - 1][j])
                        add_vert(board, i, j, list);
                    if(j == 0  || board[i][j] != board[i][j - 1])
                        add_horz(board, i, j, list);
                }
            }
            if(list.size() == 0) return board;
    
            // Crush the candies here
            for(int[] seq : list) crush(board, seq);
    
            // Drop candies
            // Start from bottom row to top row, if the space below is 0, swap
            for(int j = 0; j < n; j++) {
                for(int i = m - 1; i >= 0 ; i--) {
                    if(board[i][j] == 0)
                        continue;
                    for(int k = m - 1; k >= i; k--) {
                        if(board[k][j] == 0)
                            swap(board, i, j, k, j);
                    }
                }
            }
            return candyCrush(board);
        }
    }
    

    相关文章

      网友评论

          本文标题:Candy Crush

          本文链接:https://www.haomeiwen.com/subject/yrzrfqtx.html