Leetcode - High Five
Leetcode - Shortest Word Distance

Leetcode - Candy Crush

violet posted @ Apr 01, 2020 02:28:32 AM in 算法 with tags Algorithm Golang array , 225 阅读

https://leetcode.com/problems/candy-crush/

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:

  1. 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.
  2. 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.)
  3. After the above steps, there may exist more candies that can be crushed. If so, you need to repeat the above steps.
  4. 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.

 

If finding three consecutive elements with same abs value, start to mark all of them to 0 - val. After each round of mark, remark all negative numbers into 0. And then implement a drop.

func candyCrush(board [][]int) [][]int {
    m := len(board)
    n := len(board[0])
    found := true
    for found {
        found = false
        for i := 0; i < m; i++ {
            for j := 0; j < n; j++ {
                val := abs(board[i][j])
                if val == 0 {
                    continue
                }
                if j < n - 2 && abs(board[i][j+1]) == val && abs(board[i][j+2]) == val {
                    found = true
                    index := j
                    for index < n && abs(board[i][index]) == val {
                        board[i][index] = 0 - val
                        index++
                    }
                }
                if i < m - 2 && abs(board[i+1][j]) == val && abs(board[i+2][j]) == val {
                    found = true
                    index := i
                    for index < m && abs(board[index][j]) == val {
                        board[index][j] = 0 - val
                        index++
                    }
                }
                
            }
        }
        if found {
            for i := 0; i < m; i++ {
                for j := 0; j < n; j++ {
                    if board[i][j] < 0 {
                        board[i][j] = 0
                    }
                }
            }
            drop(board)
        }
    }
    
    return board
}

func drop(board [][]int) {
    for i := 0; i < len(board[0]); i++ {
        bottom := len(board)-1
        up := bottom -1
        for up >= 0 {
            for bottom >= 0 && board[bottom][i] != 0 {
                bottom--
            }
            up = bottom - 1
            for up >= 0 && board[up][i] == 0 {
                up--
            }
            if up < 0 {
                break
            }
            board[bottom][i] = board[up][i]
            board[up][i] = 0
            bottom--
        }
    }
}

func abs(a int) int {
    if a < 0 {
        return  0 - a
    }
    return a
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter