Zuma Game Problem


Description

LeetCode Problem 488.

You are playing a variation of the game Zuma.

In this variation of Zuma, there is a single row of colored balls on a board, where each ball can be colored red ‘R’, yellow ‘Y’, blue ‘B’, green ‘G’, or white ‘W’. You also have several colored balls in your hand.

Your goal is to clear all of the balls from the board. On each turn:

  • Pick any ball from your hand and insert it in between two balls in the row or on either end of the row.
  • If there is a group of three or more consecutive balls of the same color, remove the group of balls from the board.
  • If this removal causes more groups of three or more of the same color to form, then continue removing each group until there are none left.
  • If there are no more balls on the board, then you win the game.
  • Repeat this process until you either win or do not have any more balls in your hand.

Given a string board, representing the row of balls on the board, and a string hand, representing the balls in your hand, return the minimum number of balls you have to insert to clear all the balls from the board. If you cannot clear all the balls from the board using the balls in your hand, return -1.

Example 1:

1
2
3
4
5
6
Input: board = "WRRBBW", hand = "RB"
Output: -1
Explanation: It is impossible to clear all the balls. The best you can do is:
- Insert 'R' so the board becomes WRRRBBW. WRRRBBW -> WBBW.
- Insert 'B' so the board becomes WBBBW. WBBBW -> WW.
There are still balls remaining on the board, and you are out of balls to insert.

Example 2:

1
2
3
4
5
6
Input: board = "WWRRBBWW", hand = "WRBRW"
Output: 2
Explanation: To make the board empty:
- Insert 'R' so the board becomes WWRRRBBWW. WWRRRBBWW -> WWBBWW.
- Insert 'B' so the board becomes WWBBBWW. WWBBBWW -> WWWW -> empty.
2 balls from your hand were needed to clear the board.

Example 3:

1
2
3
4
5
6
Input: board = "G", hand = "GGGGG"
Output: 2
Explanation: To make the board empty:
- Insert 'G' so the board becomes GG.
- Insert 'G' so the board becomes GGG. GGG -> empty.
2 balls from your hand were needed to clear the board.

Example 4:

1
2
3
4
5
6
7
Input: board = "RBYYBBRRB", hand = "YRBGB"
Output: 3
Explanation: To make the board empty:
- Insert 'Y' so the board becomes RBYYYBBRRB. RBYYYBBRRB -> RBBBRRB -> RRRB -> B.
- Insert 'B' so the board becomes BB.
- Insert 'B' so the board becomes BBB. BBB -> empty.
3 balls from your hand were needed to clear the board.

Constraints:

  • 1 <= board.length <= 16
  • 1 <= hand.length <= 5
  • board and hand consist of the characters ‘R’, ‘Y’, ‘B’, ‘G’, and ‘W’.
  • The initial row of balls on the board will not have any groups of three or more consecutive balls of the same color.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution {
public:
    int findMinStep(string board, string hand) {
		sort(hand.begin(), hand.end()); // sort the hand so balls of same colour come together
        queue<string> bq; // queue for board
        queue<string> hq; // queue for hand
        queue<int> stepq; // queue for steps
        unordered_set<string> visited; // visited set for caching
        visited.insert(board + "#" + hand);
        bq.push(board);
        hq.push(hand);
        stepq.push(0); // start at step 0
        
        while (!hq.empty()) {
            string curBoard = bq.front();
            bq.pop();
            string curHand = hq.front();
            hq.pop();
            int curStep = stepq.front();
            stepq.pop();
			
			// try inserting each ball from hand at each unique position on board 
            for (int i = 0; i < curBoard.length(); i++) {
                for (int j = 0; j < curHand.length(); j++) {
				
					// the two ifs below ensures that only unique possibilities are tested, see Note 1,2
                    if (j > 0 && curHand[j] == curHand[j - 1]) continue;
                    if (i > 0 && curBoard[i - 1] == curHand[j]) continue;
					
                    bool worthTrying = false;
					
					// the if, else block below tests if this combination is worth exploring, see Note 3,4
                    if (curBoard[i] == curHand[j]) worthTrying = true;
                    else if (0 < i && curBoard[i] == curBoard[i - 1]
                      && curBoard[i] != curHand[j]) worthTrying = true;
                    
                    if (worthTrying) {
                        string newBoard = updateBoard(curBoard.substr(0, i) + curHand[j] + curBoard.substr(i), i);
                        if (newBoard == "") return curStep + 1;
                        string newHand = curHand.substr(0, j) + curHand.substr(j + 1);
                        if (visited.find(newBoard + "#" + newHand) == visited.end()) {
                            bq.push(newBoard);
                            hq.push(newHand);
                            stepq.push(curStep + 1);
                            visited.insert(newBoard + "#" + newHand);
                        }
                    }
                }
            }
        }
        return -1;
    }
    
    string updateBoard(string board, int idx) {
        if (idx < 0) return board;
        int left = idx, right = idx;
        while (left > 0 && board[left] == board[left - 1]) left--;
        while (right < board.length() && board[right] == board[right + 1]) right++;
        
        int sameClrLen = right - left + 1;
        if (sameClrLen >= 3)
            return updateBoard(board.substr(0, left) + board.substr(right + 1), left - 1);
        else
            return board;
    }
};




Related Posts

Snakes And Ladders Problem

LeetCode 909. You are given an n x n integer...

Rotting Oranges Problem

LeetCode 994. You are given an m x n grid...

Numbers With Same Consecutive Differences Problem

LeetCode 967. Return all non-negative integers of length n such...

K-Similar Strings Problem

LeetCode 854. Strings s1 and s2 are k-similar (for some...