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:
- 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.
- The number of balls on the table won't exceed 20, and the string represents these balls is called "board" in the input.
- The number of balls in your hand won't exceed 5, and the string represents these balls is called "hand" in the input.
- Both input strings will be non-empty and only contain characters 'R','Y','B','G','W'.
BFS,因为不能重复使用,所以还要维护一个额外的hand变量
这样放到queue里面的就得是个class对象了
其实DFS也可以,当全部都消掉后返回就好了
应该从递归上进行优化,不要一个位置一个位置的乱插,要有目的性的插入
要插在能消除的位置上,每一次插入都要消掉一次,那就换个思维,不再考虑插在哪以及插入什么,转而思考当前想消掉哪块,想想要是自己玩这个游戏,应该也是这样做
public class Solution {
int min = Integer.MAX_VALUE;
Map<Character, Integer> map = new HashMap<Character, Integer>();
public int findMinStep(String board, String hand) {
for(char c : "RYBGW".toCharArray()) map.put(c, 0);
for(char c : hand.toCharArray()) map.put(c, 1+map.get(c));
min = hand.length() + 1;
dfs(board, 0);
return min == hand.length() + 1 ? -1 : min;
}
private void dfs(String board, int step) {
if(step >= min) return;
if("".equals(board)) {
min = Math.min(min, step);
return;
}
for(int i=0; i<board.length(); i++) {
int j = i + 1;
while(j<board.length() && board.charAt(j)==board.charAt(i))
j++;
int needToDismiss = 3 - (j-i);
char c = board.charAt(i);
if(map.get(c) >= needToDismiss) {
map.put(c, map.get(c)-needToDismiss);
dfs(process(board.substring(0, i)+board.substring(j)), step+needToDismiss);
map.put(c, map.get(c)+needToDismiss);
}
}
}
public String process(String s) {
while(true) {
StringBuilder sb = new StringBuilder();
for(int i=0; i<s.length(); i++) {
int j = i+1;
while(j<s.length() && s.charAt(j)==s.charAt(i))
j++;
if(j-i < 3) sb.append(s.substring(i, j));
i = j - 1; // i++还会加回去
}
if(s.equals(sb.toString())) break;
s = sb.toString();
}
return s;
}
}