1723. Candy Crush 2This question is about implementing a basic elimination algorithm for Candy Crush. 3Given 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: 4If three or more candies of the same type are adjacent vertically or horizontally, "crush" them all at the same time - these positions become empty. 5After 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.) 6After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps. 7If there does not exist more candies that can be crushed (ie. the board is stable), then return the current board. 8You need to perform the above rules until the board becomes stable, then return the current board. 9Example 1: 10Input: 11board = 12[[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]] 13Output: 14[[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]] 15Explanation: 16Note: 17The length of board will be in the range [3, 50]. 18The length of board[i] will be in the range [3, 50]. 19Each board[i][j] will initially start as an integer in the range [1, 2000]. 20题意解释: 21一次性消除board中所有可以消除的糖果,然后才下落,形成新的糖果。 22 23思路: 24标记出所有需要被crash 掉的元素 25用一个data structure去记录可以被消除的糖果的位置坐标 (检查横向、纵向相同糖果的个数,只要有一个方向有三个以上相同糖果,当前这个糖果就能被删除) 26将这些元素设置为0,并且crash 27crash 处理: 28 实际上相当于two pointers把0给移到board的顶部,设置两个pointers从board的尾部开始往上走。假如值是0, 快指针往前走;假如值非0,快指针和慢指针交换value, 慢指针和快指针都向前走一位 29 30模拟crash 的过程: 316 6 6 6 0 324 4 4 0 0 333 3 0 0 0 340 => 0 => 0 => 0 => 6 351 0 0 4 4 360 0 3 3 3 370 1 1 1 1 38Time: O((row * col)^2) 39Space: O(1) 40 41Solution (Recursion): 42Space: call stack 43class CandyCrushGame { 44 class Point{ 45 int x; 46 int y; 47 public Point(int x, int y ) { 48 this.x = x; 49 this.y = y; 50 } 51 } 52 public int[][] candyCrush(int[][] board) { 53 Set<Point> markDeleted = new HashSet<>(); 54 for (int i = 0; i < board.length; i++) { 55 for (int j = 0; j < board[0].length; j++) { 56 if (board[i][j] == 0) continue; 57 if ((i - 2 >= 0 && board[i][j] == board[i- 1][j] && board[i][j] == board[i - 2][j])|| 58 (j - 2 >= 0 && board[i][j] == board[i][j - 1] && board[i][j] == board[i][j - 2])|| 59 (i + 2 < board.length && board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j])|| 60 (j + 2 < board[0].length && board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2])|| 61 (i - 1 >= 0 && i + 1 < board.length && board[i - 1][j] == board[i][j] && board[i][j] == board[i + 1][j])|| 62 (j - 1 >= 0 && j + 1 < board[0].length && board[i][j - 1] == board[i][j] && board[i][j] == board[i][j + 1])) { 63 markDeleted.add(new Point(i, j)); 64 } 65 } 66 } 67 if (markDeleted.isEmpty()) return board; 68 for(Point p: markDeleted) { 69 board[p.x][p.y] = 0; 70 } 71 72 dropBoard(board); 73 return candyCrush(board); 74 } 75 76 private void dropBoard(int[][] board) { 77 for (int j = 0; j < board[0].length; j++) { 78 int bot = board.length - 1; 79 int top = board.length - 1; 80 while (top >= 0) { 81 if (board[top][j] == 0) { 82 top--; 83 } 84 else { 85 board[bot--][j] = board[top--][j]; 86 } 87 } 88 while (bot >= 0) { 89 board[bot--][j] = 0; 90 } 91 } 92 } 93} 94Solution (Iterative): 95class Point { 96 int x; 97 int y; 98 public Point(int x, int y) { 99 this.x = x; 100 this.y = y; 101 } 102 } 103 public int[][] candyCrush(int[][] board) { 104 int m = board.length; 105 int n = board[0].length; 106 107 while (true) { 108 List<Point> deletion = new ArrayList<>(); 109 for (int i = 0; i < m; i++) { 110 for (int j = 0; j < n; j++) { 111 if (board[i][j] == 0) continue; 112 int x0 = i; 113 int x1 = i; 114 int y0 = j; 115 int y1 = j; 116 while (x0 >= 0 && x0 > i - 3 && board[x0][j] == board[i][j]) --x0; 117 while (x1 < m && x1 < i + 3 && board[x1][j] == board[i][j]) ++x1; 118 while (y0 >= 0 && y0 > j - 3 && board[i][y0] == board[i][j]) --y0; 119 while (y1 < n && y1 < j + 3 && board[i][y1] == board[i][j]) ++y1; 120 if (x1 - x0 > 3 || y1 - y0 > 3) deletion.add(new Point(i, j)); 121 } 122 } 123 if (deletion.size() == 0) break; 124 for (Point p: deletion) { 125 board[p.x][p.y] = 0; 126 } 127 for (int j = 0; j < n; j++) { 128 int t = m - 1; 129 for (int i = m - 1; i >= 0; i--) { 130 if (board[i][j] != 0) { 131 int tmp = board[t][j]; 132 board[t][j] = board[i][j]; 133 board[i][j] = tmp; 134 t--; 135 } 136 } 137 } 138 } 139 return board; 140 }
723. Candy Crush
Stella981
2021-10-11
910 0 0
点赞
收藏
评论区
加载中...