美文网首页
Candy Crush

Candy Crush

作者: Frank_Kivi | 来源:发表于2018-06-27 10:12 被阅读15次

https://www.lintcode.com/problem/candy-crush/description

public class Solution {
    /**
     * @param board: a 2D integer array
     * @return: the current board
     */
    public int[][] candyCrush(int[][] board) {
        // Write your code here
        boolean[][] visited = new boolean[board.length][board[0].length];
        while (true) {
            int count = 0;
            for (int i = 0; i < board.length; i++) {
                int[] ints = board[i];
                int index = 0;
                while (index < ints.length) {
                    if (index + 2 < ints.length && ints[index] == ints[index + 1] && ints[index]
                            == ints[index + 2] && ints[index] != 0) {
                        int temp = index;
                        while (temp < ints.length && ints[temp] == ints[index]) {
                            visited[i][temp] = true;
                            temp++;
                            count++;
                        }
                        index = temp;
                    } else {
                        index++;
                    }
                }
            }
            for (int i = 0; i < board[0].length; i++) {
                int index = 0;
                while (index < board.length) {
                    if (index + 2 < board.length && board[index][i] == board[index + 1][i] &&
                            board[index][i]
                                    == board[index + 2][i] && board[index][i] != 0) {
                        int temp = index;
                        while (temp < board.length && board[temp][i] == board[index][i]) {
                            visited[temp][i] = true;
                            temp++;
                            count++;
                        }
                        index = temp;
                    } else {
                        index++;
                    }
                }
            }
            if (count == 0) {
                return board;
            }
            for (int i = 0; i < visited.length; i++) {
                boolean[] booleans = visited[i];
                for (int j = 0; j < booleans.length; j++) {
                    if (booleans[j]) {
                        booleans[j] = false;
                        int index = i;
                        board[index][j] = 0;
                        while (index > 0) {
                            int temp = board[index][j];
                            board[index][j] = board[index - 1][j];
                            board[index - 1][j] = temp;
                            index--;
                        }
                    }
                }
            }
        }
    }
}

相关文章

  • 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/epfbyftx.html