# [LeetCode daily question] (Xiaole puzzle) 488. Zuma game

The official suddenly changed the question, and there was no clear problem to solve

# subject

You are participating in a variant of Zuma game.

In this Zuma game variant, there is a row of colored balls on the desktop. The color of each ball may be red 'R', yellow 'Y', blue 'B', green 'G' or white 'W'. You also have some colored balls in your hand.

Your goal is to empty all the balls on the table. Each round:

Choose any one of the colored balls in your hand and insert it into the volleyball on the table: between the two balls or at either end of the volleyball.
Then, if there are three or more balls of the same color connected, remove them.
If this removal operation also causes three or more balls of the same color to be connected, you can continue to remove these balls until the removal conditions are no longer met.
If all the balls on the table are removed, you are considered to have won the game.
Repeat this process until you win the game or have no more balls in your hand.
Give you a string board, which represents the volleyball at the beginning on the desktop. Another string hand is given to you to represent the colored ball in your hand. Please follow the above steps to remove all the balls on the table, calculate and return the minimum number of balls required. If you cannot remove all the balls on the table, return - 1.

Example 1:

Input: board = "WRRBBW", hand = "RB"
Output: - 1
Explanation: you cannot remove all balls from the desktop. The best situation available is:
-Insert an 'R' to change the desktop to wrrrbbw. WRRRBBW -> WBBW
-Insert a 'B' to make the desktop wbbbw. WBBBW -> WW
There are balls left on the table. No other balls can be inserted.

Example 2:

Input: board = "WWRRBBWW", hand = "WRBRW"
Output: 2
Explanation: to empty the ball on the desktop, follow the steps below:
-Insert an 'R' to change the desktop to wwrrrbbww. WWRRRBBWW -> WWBBWW
-Insert a 'B' to change the desktop to wwbbbww. WWBBBWW -> WWWW -> empty
Just take 2 balls out of your hand to empty the desktop.

Example 3:

Input: board = "g", hand = "gggggg"
Output: 2
Explanation: to empty the ball on the desktop, follow the steps below:
-Insert a 'G' to change the desktop to GG.
-Insert a 'G' to change the desktop to GGG. GGG -> empty
Just take 2 balls out of your hand to empty the desktop.

Example 4:

Input: board = "RBYYBBRRB", hand = "YRBGB"
Output: 3
Explanation: to empty the ball on the desktop, follow the steps below:
-Insert a 'Y' to change the desktop to rbyybbrrb. RBYYYBBRRB -> RBBBRRB -> RRRB -> B
-Insert a 'B' to change the desktop to BB.
-Insert a 'B' to make the desktop BBB. BBB -> empty
Just three balls from your hand can empty the desktop.

Tips:

1 <= board.length <= 16
1 <= hand.length <= 5
board and hand consist of the characters' R ',' Y ',' B ',' G 'and' W '
There will not be three or more balls with the same color and connected at the beginning of the ball on the desktop

Source: LeetCode

# 1, Problem solving ideas

Traverse all the balls in your hand. If the current ball has been used, it will no longer be used. If the current ball has not been used, use the current ball to launch, and the state compression is marked as used. Enumerate all insertion positions, simplify enumeration by pruning, and eliminate balls of the same color with double pointers.

Refer to the code and notes for the specific process. For backtracking and pruning methods, see Force buckle

# 2, Results

## 1. Precautions

Enumerate the positions to be inserted and prune. eg for RRWW, if the ball in hand is R, the insertion of the first position and the second position is the same, so as to avoid repetition;

In addition, if the color of the selected ball is different from that of the inserted ball, it is not necessary to continue. Even if continuous elimination occurs, there must be at least two same colors, such as board=RRWWR, hand=W. it is meaningful only if W is inserted near W. in case of RRWR, no matter where W is inserted, it is meaningless

## 2.Java code

The code is as follows (example):

class Solution {
private static final int MAX = 0x3F3F3F3F;

public int findMinStep(String board, String hand) {
//1 < < hand.length: the usage of all balls, initially unused, is represented by 0
int ans = memoization(board, hand, new HashMap<>(), 1 << hand.length());
return ans == MAX ? -1 : ans;
}

private int memoization(String board, String hand, Map<String, Integer> cache, int cur) {
if (board.length() == 0) return 0;
if (cache.containsKey(board)) return cache.get(board);
int ans = MAX;
//Traverse all the balls in your hand
for (int i = 0; i < hand.length(); i++) {
//If the current ball has been used, it is no longer used
if (((cur >> i) & 1) == 1) continue;
//The current ball has not been used. Use the current ball to launch, and the state compression is marked as used
int next = (1 << i) | cur;
//Enumerate all insertion locations
for (int j = 0; j <= board.length(); j++) {
//Pruning: for RRWW, if the ball in hand is R, the situation of inserting into the first position and the second position is the same
if (j > 0 && j < board.length() - 1 && board.charAt(j) == board.charAt(j - 1)) continue;
//For pruning, if the color of the selected ball is different from that of the inserted ball, it is not necessary to continue. Even if continuous elimination occurs, there must be at least two same colors, such as board=RRWWR, hand=W. it is meaningful only if W is inserted near W. in case of RRWR, it is meaningless no matter where W is inserted
if (j > 0 && j < board.length() - 1 && board.charAt(j) != hand.charAt(i)) continue;
//curBoard records the situation after inserting the current ball
StringBuilder curBoard = new StringBuilder();
curBoard.append(board, 0, j).append(hand.charAt(i));
if (j != board.length()) curBoard.append(board.substring(j));
//Double pointers are used to eliminate balls of the same color. StringBuilder is passed by reference and does not need to be updated with the returned results
eliminateSameColor(curBoard, j);
ans = Math.min(ans, memoization(curBoard.toString(), hand, cache, next) + 1);
}
}
cache.put(board, ans);
return ans;
}

private void eliminateSameColor(StringBuilder curBoard, int i) {
//Diffusion elimination from i position
while (i >= 0 && i < curBoard.length()) {
int left = i, right = i;
char c = curBoard.charAt(i);
while (left >= 0 && curBoard.charAt(left) == c) {
left--;
}
while (right < curBoard.length() && curBoard.charAt(right) == c) {
right++;
}
//If there are 3 or more balls of the same color, eliminate them
if (right - left > 3) {
curBoard.delete(left + 1, right);
i = left >= 0 ? left : right;
} else {
break;
}
}
}
}

# summary

It's so difficult, meet the difficulties, come on, come on!!!!!

Tags: Algorithm leetcode

Posted on Tue, 09 Nov 2021 17:13:31 -0500 by Rhiknow