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.
Example 1:
Input:
board =
[[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]]
Output:
[[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]]
Explanation:
Note:
The length of board will be in the range [3, 50].
The length of board[i] will be in the range [3, 50].
Each board[i][j] will initially start as an integer in the range [1, 2000].
answer one
The idea is to traverse the entire matrix again and again to remove crush until no crush can be found.
For each traversal of the matrix, we only check two directions, rightward and downward. There is no need to check upward and leftward because they would have been checked from previous cells.
For each cell, if there are at least three candies of the same type rightward or downward, set them all to its negative value to mark them.
After each traversal, we need to remove all those negative values by setting them to 0, then let the rest drop down to their correct position. A easy way is to iterate through each column, for each column, move positive values to the bottom then set the rest to 0.
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;
}
}
answer two
I found this solution from http://storypku.com/2017/11/leetcode-question-723-candy-crush/, and I convert it to Java. It’s not the shortest, but the logic is clear to me.
The idea is that we try to crush the candy horizontally, then vertically, and drop them vertically (because we have to fill the empty spots). The way we mark whether a candy needs to be crushed(set to 0) is to set an opposite value to it, so that we don’t have to maintain another data structure.
public int[][] candyCrush(int[][] board) {
int m = board.length;
int n = board[0].length;
boolean shouldContinue = false;
// Crush horizontally
for (int i = 0; i < m; i++) {
for (int j = 0; j < n - 2; j++) {
int v = Math.abs(board[i][j]);
if (v > 0 && v == Math.abs(board[i][j + 1]) && v == Math.abs(board[i][j + 2])) {
board[i][j] = board[i][j + 1] = board[i][j + 2] = -v;
shouldContinue = true;
}
}
}
// Crush vertically
for (int i = 0; i < m - 2; i++) {
for (int j = 0; j < n; j++) {
int v = Math.abs(board[i][j]);
if (v > 0 && v == Math.abs(board[i + 1][j]) && v == Math.abs(board[i + 2][j])) {
board[i][j] = board[i + 1][j] = board[i + 2][j] = -v;
shouldContinue = true;
}
}
}
// Drop vertically
for (int j = 0; j < n; j++) {
int r = m - 1;
for (int i = m - 1; i >= 0; i--) {
if (board[i][j] >= 0) {
board[r--][j] = board[i][j];
}
}
for (int i = r; i >= 0; i--) {
board[i][j] = 0;
}
}
return shouldContinue ? candyCrush(board) : board;
}
answer three
The idea is not to count how many same “candies” are in a row or column, but to check if this candy is eligible for crushing. If any candy is eligible, store the corresponding coordinates in a HashSet.
After traversing the whole board, set the valid candies to “0” then crush (using 2-pointer method in a column).
Here goes the code:
class Solution {
public int[][] candyCrush(int[][] board) {
Set<Coordinates> set = new HashSet<>();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
int cur = board[i][j];
if (cur == 0) continue;
if ((i - 2 >= 0 && board[i - 1][j] == cur && board[i - 2][j] == cur) || // check left 2 candies
(i + 2 <= board.length - 1 && board[i + 1][j] == cur && board[i + 2][j] == cur) || // check right 2 candies
(j - 2 >= 0 && board[i][j - 1] == cur && board[i][j - 2] == cur) || // check 2 candies top
(j + 2 <= board[i].length - 1 && board[i][j + 1] == cur && board[i][j + 2] == cur) || // check 2 candies below
(i - 1 >= 0 && i + 1 <= board.length - 1 && board[i - 1][j] == cur && board[i + 1][j] == cur) || // check if it is a mid candy in row
(j - 1 >= 0 && j + 1 <= board[i].length - 1 && board[i][j - 1] == cur && board[i][j + 1] == cur)) { // check if it is a mid candy in column
set.add(new Coordinates(i, j));
}
}
}
if (set.isEmpty()) return board; //stable board
for (Coordinates c : set) {
int x = c.i;
int y = c.j;
board[x][y] = 0; // change to "0"
}
drop(board);
return candyCrush(board);
}
private void drop(int[][] board) { // using 2-pointer to "drop"
for (int j = 0; j < board[0].length; j++) {
int bot = board.length - 1;
int top = board.length - 1;
while (top >= 0) {
if (board[top][j] == 0) {
top--;
}
else {
board[bot--][j] = board[top--][j];
}
}
while (bot >= 0) {
board[bot--][j] = 0;
}
}
}
}
class Coordinates {
int i;
int j;
Coordinates(int x, int y) {
i = x;
j = y;
}
}
answer four
Great idea! Slightly more concise version without Coordinates class and the drop method:
public int[][] candyCrush(int[][] a) {
List<int[]> crush = new LinkedList<>();
int n = a.length, m = a[0].length;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (a[i][j] != 0) {
int v = a[i][j];
if (j + 2 < m && v == a[i][j + 1] && v == a[i][j + 2])
for (int k = j; k < m && a[i][k] == v; k++)
crush.add(new int[]{i, k});
if (i + 2 < n && v == a[i + 1][j] && v == a[i + 2][j])
for (int k = i; k < n && a[k][j] == v; k++)
crush.add(new int[]{k, j});
}
if (crush.isEmpty()) return a;
for (int[] c : crush)
a[c[0]][c[1]] = 0;
for (int j = 0; j < m; j++) {
int bot = n - 1, top = n - 1;
while (top >= 0)
if (a[top][j] == 0)
top--;
else a[bot--][j] = a[top--][j];
while (bot >= 0)
a[bot--][j] = 0;
}
return candyCrush(a);
}
```