[LeetCode] Zuma Game

本文探讨了Zuma Game的玩法,你需要将手中的球插入到桌面上的球列中,形成至少三个相同颜色相邻的球组并消除它们。通过递归深度优先搜索(DFS)算法来寻找移除所有桌面球的最小插入次数。如果无法清除所有球,则返回-1。题目保证初始桌面上不会有三个以上连续同色球,并限制了桌面和手中的球数量以及输入字符串的格式。
摘要由CSDN通过智能技术生成

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

dfs算法解题:

public class Solution {
	 public int findMinStep(String board, String hand) {
		 HashMap<Character,Integer> map=new HashMap<Character,Integer>();
		 for(int i=0;i<hand.length();i++){
			Integer num=map.get(hand.charAt(i));
			if(num==null) num=0;
			map.put(hand.charAt(i),num+1);
		 }
//		 board=clearTriple(board);
		 dfs(board,map);
		 return re==Integer.MAX_VALUE?-1:re;
	 }
	 int re=Integer.MAX_VALUE;
	 int trace=0;
	 public void dfs(String s,HashMap<Character,Integer> map){
		 if(s.length()==0){
			 re=Math.min(trace,re);
			 return;
		 }
		 int j=0;
		 for(int i=0;i<=s.length();i++){
			 if(i!=s.length()&&s.charAt(i)==s.charAt(j)) continue;
			 int numInMap=map.containsKey(s.charAt(j))?map.get(s.charAt(j)):0;
			 if(i-j+numInMap>=3){
				 if(i-j>=3){
					 dfs(s.substring(0,j)+s.substring(i),map);
				 }else{
					 map.put(s.charAt(j),numInMap-3+i-j);
					 trace+=3-i+j;
					 dfs(s.substring(0,j)+s.substring(i),map);
					 trace-=3-i+j;
					 map.put(s.charAt(j), numInMap);
				 }
			 }
			 j=i;
		 } 
	 }
//	 public String clearTriple(String s){
//		 int start=0,i=0;
//		 while(i<=s.length()){
//			 if(i==s.length()||s.charAt(i)!=s.charAt(start)){
//				 if(i-start>=3) return clearTriple(s.substring(0,start)+s.substring(i));
//				 else start=i;
//			 }
//			 i++;
//		 }
//	     return s;
//	 }
	 public static void main(String[] args) {
		Solution s=new Solution();
		System.out.println(s.findMinStep("WWRRBBWW","WRBRW"));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值