美文网首页
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.lintcode.com/problem/candy-crush/description

  • Candy Crush

    题目This question is about implementing a basic elimination...

  • 《Candy Crush Friends》

    根据之前看到Match 3 Meta layers and match types,我们看到文章中提到,这么几个趋...

  • Candy crush的世界

    我不是一个很爱玩游戏的女孩,但是对于candy crush,我从出国到回国,只要一有时间就会玩一玩这个游戏。可...

  • King教你打造三消游戏领跑者

    Candy Crush Saga是一款英国游戏开发商King旗下的微策略消除类游戏。自面世以来便迅速占领Apple...

  • TA是我没有生命的好朋友

    当我坐在椅子上,捧着手机好几个小时,我并不是在思考人生。我是在打游戏。《Candy crush》这个超过1000关...

  • 糖果传奇:当时间加上期限

    在《Candy Crush糖果传奇》中,有一种关卡是我超级不喜欢的,那就是计时的关卡。秒表不断倒计时,必须在时间之...

  • iOS --不联网状态防止改时间作弊思路

    一、作弊方法描述 一些游戏会在某些情况下让玩家等待一段时间,例如candy crush中游戏失败会消耗一点体力,体...

  • #西游记#

    其实这是一篇关于西语和Candy Crush的文字游戏。得出的结论是:没有那七十二变的本事就选择三十六计走为上计吧...

  • 糖果传奇

    玩家们“沉迷”于Candy Crush的9大原因 这里边只有9能获得我的一丝认同只是一丝,我只需要一个游戏来放空我...

网友评论

      本文标题:Candy Crush

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