Problem Statement
You are given a 2D grid of candies represented as a List<List<String>>
, where each string denotes the type and color of the candy. The grid follows these rules:
A candy is represented by a color:
- Colors:
r
(red),g
(green),b
(blue),y
(yellow)
- Colors:
When 3 or more adjacent candies of the same color (in a row or column) are found, they "crush" and become empty (
""
). After crushing, candies above fall down to fill the empty spots, and new candies (random colors) appear at the top.
Your task is to simulate one step of the candy crush game: find and crush all valid groups, shift candies down, and print the updated grid.
Input Format
- A single integer
n
(2 ≤n
≤ 10) representing the number of rows. - A single integer
m
(2 ≤m
≤ 10) representing the number of columns. - Followed by
n
lines, each containingm
space-separated strings representing the initial grid of candies.
- Print the updated grid after one round of crushing and shifting. Each row of the grid should be printed on a new line.
Sample Input 0
Explanation 0
Row-wise crushing:
- In row 1, we crushed the group of
g g g
. - In row 3, we crushed the group of
b b b
. - In row 4, we crushed the group of
y y y
.
Shift down:
- After crushing, the remaining candies fall down to fill the empty spots.
- The final grid has
.
at the top and the remaining candies (b
andy
) shifted to the bottom.
Sample Input 1
Features
- Row and Column Detection: Checks for horizontal and vertical matches.
- Dynamic Grid: Works for grids of different sizes (
n x m
). - Crush and Shift: Marks candies to be crushed and shifts remaining candies down.
Insights
- The program begins by reading the grid dimensions
n
(rows) andm
(columns) from input. - The grid is then populated using a 2D array, where each element represents a candy color.
- A boolean 2D array
toCrush
is used to track which candies need to be crushed. - The program checks for horizontal groups of 3 consecutive candies of the same color (row-wise).
- It marks candies for crushing in the
toCrush
array if they form a group of 3 or more. - A similar check is performed for vertical groups of 3 consecutive candies (column-wise).
- After identifying candies to crush, the program shifts non-crushed candies downward in each column.
- For each column, the program writes non-crushed candies starting from the bottom.
- Empty spaces are filled with
"."
to represent the crushed candies after the shift. - The final grid is printed, showing the updated candy arrangement with crushed spots replaced by
"."
.
Debugging is like clearing a row of candies in Candy Crush—sometimes you just need to match the right pieces to make everything fall into place. Keep coding, and let those errors disappear like crushed candies!
Enjoy the game of colors along with coding!!! 🟡🟢🔴🧑💻🟠👩💻🟣🟤🔵
Comments
Post a Comment