Candy Crush
This problem involves iteratively crushing candies that are adjacent vertically or horizontally (3 or more), and then allowing candies above the crushed ones to drop down. This process continues until no more candies can be crushed.
Here’s a step-by-step explanation of the code to solve this problem:
Find Candies to Crush: For each cell in the board, check if it’s part of a horizontal or vertical line of three or more of the same candy. If so, mark it for crushing. This can be done by setting a temporary board with the negative value of the candy that will be crushed.
Crush the Candies: Iterate through the board, and for each cell marked for crushing, set it to zero.
Drop the Candies: Iterate through each column from the bottom up, shifting non-zero elements downwards.
Repeat Until Stable: Repeat steps 1-3 until no more candies can be crushed.
Python solution:
|
|
This code will handle the entire process of identifying crushable candies, crushing them, dropping the candies above, and repeating this process until the board is stable.