美文网首页
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.haomeiwen.com/subject/epfbyftx.html