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:
1. 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.
2. 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.)
3. After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.
4. 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.
Note:
1. The length of board will be in the range [3, 50].
2. The length of board[i] will be in the range [3, 50].
3. Each board[i][j] will initially start as an integer in the range [1, 2000].
实现题。
1.每一轮先扫一遍找到所有可以消除的糖果。只向右和向下找,标为负数。
2.实施drop。对每一列把正数挪到最下面,然后上面剩下的set为0.
3.如果这一轮没有发现任何可以消除的糖果就结束循环了。
细节:
1.标为负数比先用额外boolean[][]记录再改为0要少循环一些。不过这样你对比格子的时候就要对比abs值。
2.注意遇到0就跳过处理,避免出现看到3个连着的0也标要消除从而无线循环了。
lc优秀网友代码:
class Solution { public int[][] candyCrush(int[][] board) { int N = board.length, M = board[0].length; boolean found = true; while (found) { found = false; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { int val = Math.abs(board[i][j]); if (val == 0) continue; if (j < M - 2 && Math.abs(board[i][j + 1]) == val && Math.abs(board[i][j + 2]) == val) { found = true; int ind = j; while (ind < M && Math.abs(board[i][ind]) == val) board[i][ind++] = -val; } if (i < N - 2 && Math.abs(board[i + 1][j]) == val && Math.abs(board[i + 2][j]) == val) { found = true; int ind = i; while (ind < N && Math.abs(board[ind][j]) == val) board[ind++][j] = -val; } } } if (found) { // move positive values to the bottom, then set the rest to 0 for (int j = 0; j < M; j++) { int storeInd = N - 1; for (int i = N - 1; i >= 0; i--) { if (board[i][j] > 0) { board[storeInd--][j] = board[i][j]; } } for (int k = storeInd; k >= 0; k--) board[k][j] = 0; } } } return board; } }
我的渣渣代码:
class Solution { private int[][] board; public int[][] candyCrush(int[][] board) { this.board = board; boolean isStable = false; while (!isStable) { isStable = true; boolean[][] mark = new boolean[board.length][board[0].length]; for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (shouldCrush(i, j)) { mark[i][j] = true; isStable = false; } } } for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (mark[i][j]) { board[i][j] = 0; } } } drop(); } return board; } private void drop() { int height = board.length; for (int j = 0; j < board[0].length; j++) { // 指针从图像的下往上,其实是代码里的大往小。 int up = board.length - 1, down = board.length - 1; while (up >= 0 && board[up][j] != 0) { up--; down--; } while (up >= 0) { while (up >= 0 && board[up][j] == 0) { up--; } while (down >= 0 && board[down][j] != 0) { down--; } if (isValid(up, j) && isValid(down, j)) { int temp = board[up][j]; board[up][j] = board[down][j]; board[down][j] = temp; up--; down--; } } } } private boolean shouldCrush(int x, int y) { // P1: 为0不用处理,不要把三个连0也消了。 if (board[x][y] == 0) { return false; } int crt = board[x][y]; int xCnt = 1, yCnt = 1; int i = 1; while (xCnt < 3 && isValid(x, y + i) && board[x][y + i++] == crt) { xCnt++; } i = -1; while (xCnt < 3 && isValid(x, y + i) && board[x][y + i--] == crt) { xCnt++; } i = 1; while (yCnt < 3 && isValid(x + i, y) && board[x + i++][y] == crt) { yCnt++; } i = -1; while (yCnt < 3 && isValid(x + i, y) && board[x + i--][y] == crt) { yCnt++; } return xCnt >= 3 || yCnt >= 3; } private boolean isValid(int x, int y) { return x >= 0 && x < board.length && y >= 0 && y < board[0].length; } }