---
# System prepended metadata

title: 0723. Candy Crush
tags: [Leetcode, Bloomberg, Medium]

---

# 0723. Candy Crush
###### tags: `Leetcode` `Medium` `Bloomberg`
Link: https://leetcode.com/problems/candy-crush/
## 思路
就按照要求一步步来就可以
但这道题不需要用到dfs，不能中毒太深
用到dfs是因为对于某个点 上下左右都要找一遍来搜索结果
但这道题每次只需要沿着一个方向找就可以了
直接用回圈就可以做~
## Code
```java=
class Solution {
    public int[][] candyCrush(int[][] board) {
        boolean change = false;
        for(int i = 0;i < board.length;i++){
            for(int j = 0;j < board[0].length-2;j++){
                int num =  Math.abs(board[i][j]);
                if(num!=0 && num == Math.abs(board[i][j+1]) && num == Math.abs(board[i][j+2])){
                    board[i][j] = board[i][j+1] = board[i][j+2] = -num;
                    change = true;
                }
            }
        }
        
        for(int i = 0;i < board[0].length;i++){
            for(int j = 0;j < board.length-2;j++){
                int num =  Math.abs(board[j][i]);
                if(num!=0 && num == Math.abs(board[j+1][i]) && num == Math.abs(board[j+2][i])){
                    board[j][i] = board[j+1][i] = board[j+2][i] = -num;
                    change = true;
                }
            }
        }
        
        int diff = 0;
        if(change){
            for(int i = 0;i < board[0].length;i++){
                diff = 0;
                for(int j = board.length-1;j >= 0;j--){
                    if(board[j][i]<0){
                        board[j][i] = 0;
                        diff++;
                    }
                    else if(diff!=0){
                        board[j+diff][i] = board[j][i];
                        board[j][i] = 0;
                    }
                }
            }
        }
        return change?candyCrush(board):board;
    }
}
```