【leetcode】488. Zuma Game

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ghscarecrow/article/details/86657307

Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y), blue(B), green(G), and white(W). You also have several balls in your hand.

Each time, you may choose a ball in your hand, and insert it into the row (including the leftmost place and rightmost place). Then, if there is a group of 3 or more balls in the same color touching, remove these balls. Keep doing this until no more balls can be removed.

Find the minimal balls you have to insert to remove all the balls on the table. If you cannot remove all the balls, output -1.

 

Examples:
Input: "WRRBBW", "RB" Output: -1 Explanation: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW Input: "WWRRBBWW", "WRBRW" Output: 2 Explanation: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty Input:"G", "GGGGG" Output: 2 Explanation: G -> G[G] -> GG[G] -> empty Input: "RBYYBBRRB", "YRBGB" Output: 3 Explanation: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty

Note:

  1. You may assume that the initial row of balls on the table won’t have any 3 or more consecutive balls with the same color.
  2. The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
  3. The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
  4. Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.

题解如下:

class Solution {
    int MAX_COUNT = 6;
    public int findMinStep(String board, String hand) {
        int[] hands = new int[26];//存储手中持有的球,因为有26个字母,所以用26个下标,方便寻址
        for(int i = 0;i < hand.length();i++) {
            //初始化数据
            char c = hand.charAt(i);
            hands[c - 'A']++;
        }
        int res = backtrack(board + "#",hands);
        return res == MAX_COUNT? -1:res;
    }
    
    public int backtrack(String board,int[] hands) {
        board = removeBalls(board);
        if(board.equals("#")) {
            return 0;
        }
        int need = 0;
        int res = MAX_COUNT;
        for(int i = 0,j = 0;j < board.length();j++) {
            if(board.charAt(i) == board.charAt(j))
                continue;
            need = 3 - (j - i);
            //如果手头有该类球且能满足需要
            if(hands[board.charAt(i) - 'A'] >= need) {
                hands[board.charAt(i) - 'A'] -= need;
                res = Math.min(res,need + backtrack(board.substring(0,i) + board.substring(j),hands));
                hands[board.charAt(i) - 'A'] += need;
            }
            i = j;
        }
        return res;
    }
    
    //移除满足消去条件的序列
    public String removeBalls(String board) {
        for(int i = 0,j = 0;j < board.length();j++) {
            if(board.charAt(i) == board.charAt(j)) {
                continue;
            }
            if(j - i >= 3) {
                return removeBalls(board.substring(0,i) + board.substring(j));
            } else {
                i = j;
            }
        }
        return board;
    }
}

猜你喜欢

转载自blog.csdn.net/ghscarecrow/article/details/86657307