|
11 | 11 |
|
12 | 12 | ## [723. Candy Crush (Medium)](https://leetcode.com/problems/candy-crush "粉碎糖果")
|
13 | 13 |
|
| 14 | +<p>This question is about implementing a basic elimination algorithm for Candy Crush.</p> |
14 | 15 |
|
| 16 | +<p>Given a 2D integer array <code>board</code> representing the grid of candy, different positive integers <code>board[i][j]</code> represent different types of candies. A value of <code>board[i][j] = 0</code> represents that the cell at position <code>(i, j)</code> 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 <i>stable state</i> by crushing candies according to the following rules:</p> |
| 17 | + |
| 18 | +<ol> |
| 19 | + <li>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.</li> |
| 20 | + <li>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.)</li> |
| 21 | + <li>After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.</li> |
| 22 | + <li>If there does not exist more candies that can be crushed (ie. the board is <i>stable</i>), then return the current board.</li> |
| 23 | +</ol> |
| 24 | + |
| 25 | +<p>You need to perform the above rules until the board becomes stable, then return the current board.</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | + |
| 29 | +<p><b>Example:</b></p> |
| 30 | + |
| 31 | +<pre style="white-space: pre-line"> |
| 32 | +<b>Input:</b> |
| 33 | +board = |
| 34 | +[[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]] |
| 35 | + |
| 36 | +<b>Output:</b> |
| 37 | +[[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]] |
| 38 | + |
| 39 | +<b>Explanation:</b> |
| 40 | +<img src="https://assets.leetcode.com/uploads/2018/10/12/candy_crush_example_2.png" style="width: 777px; height: 532px;" /> |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | + |
| 45 | +<p><b>Note:</b></p> |
| 46 | + |
| 47 | +<ol> |
| 48 | + <li>The length of <code>board</code> will be in the range [3, 50].</li> |
| 49 | + <li>The length of <code>board[i]</code> will be in the range [3, 50].</li> |
| 50 | + <li>Each <code>board[i][j]</code> will initially start as an integer in the range [1, 2000].</li> |
| 51 | +</ol> |
15 | 52 |
|
16 | 53 | ### Related Topics
|
17 | 54 | [[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)]
|
|
0 commit comments