488. Zuma Game

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'.


思路:因为是求最小的次数,考虑用BFS
BFS,因为不能重复使用,所以还要维护一个额外的hand变量
这样放到queue里面的就得是个class对象了

其实DFS也可以,当全部都消掉后返回就好了
DFS prune还是TLE
应该从递归上进行优化,不要一个位置一个位置的乱插,要有目的性的插入
要插在能消除的位置上,每一次插入都要消掉一次,那就换个思维,不再考虑插在哪以及插入什么,转而思考当前想消掉哪块,想想要是自己玩这个游戏,应该也是这样做

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;
	}
}


以下是祖玛游戏的代码,使用C++实现: ```c++ #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> using namespace std; int n, ans; // n表示小球的个数,ans表示最少需要几次操作 char s[200]; int find(int l, int r) // 找到最长的一段相同颜色的小球 { int i = l, j = r; while (i < j) { while (i < j && s[i] == s[i+1]) i++; while (i < j && s[j] == s[j-1]) j--; if (i < j && s[i] == s[j]) i++, j--; else break; } return i; } void solve() { ans = n; for (int i = 0; i < n; i++) { int j = find(i, n-1); if (j < n-1) // 如果有可以消除的小球 { int k; for (k = j+1; k < n; k++) if (s[k] != s[j]) break; // 找到第一个与s[j]颜色不同的小球 for (int l = k-j; l >= 0; l--) { // 枚举将s[j]和后面的l个小球消除 int start = max(0, i-l); // left int end = min(n-1, j+1); // right for (int r = start; r <= end; r++) { int t = find(start, end); char c = s[t]; int len = t - r + 1; for (int p = t+1; p < n; p++) s[p-len] = s[p]; // 将后面的小球向前移len个位置 n -= len; ans = min(ans, l+1 + solve()); // 递归 n += len; for (int p = n-1; p >= t+1-len; p--) s[p+len] = s[p]; // 将小球复原 for (int p = t-l+1; p <= t; p++) s[p] = c; } } break; } } } int main() { while (cin >> s && s[0] != 'E') { n = strlen(s); solve(); cout << ans << endl; } return 0; } ``` 该代码使用了分治法,对于每个区间,找到其中最长的一段相同颜色的小球,然后枚举将这一段小球和之后的小球一起消除的情况,递归求解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值