关闭

leetcode 488. Zuma Game(祖玛游戏)

317人阅读 评论(0) 收藏 举报
分类:

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'.
又是一道游戏题,这次是祖玛游戏。其实这道题本身是出的有问题的。如下面这个测试用例。详细见https://discuss.leetcode.com/topic/76832/standard-test-program-is-wrong/9

Input: "RRWWRRBBRR", "WB". The test program gave the expected answer -1. However, I thought the answer might be 2. Because:

RRWWRRBBRR -> RRWWRRBBR[W]R -> RRWWRRBB[B]RWR -> RRWWRRRWR -> RRWWWR -> RRR -> empty

因此,我觉得题干中应该加上限制:”球只能插入进相同颜色的球旁边" 。

我的思路很简单,我就每次看其中连续的一串数。

如 WRRBBW。
假设把 W 填满,那么看 hands 中有没有两个 W,如果有,则填满 W 并减去 hands 中的两个 W,继续递归看 RRBBW 。递归...
再假设把 R 填满,那么看 hands 中有没有一个 R,如果有,则填满 R 并减去 hands 中的一个 R,继续递归看 WBBW 。递归...
再假设把 B 填满,那么看 hands 中有没有一个 B,如果有,则填满 B 并减去 hands 中的一个 B,继续递归看 WRRW 。递归...
再假设把 W 填满,那么看 hands 中有没有两个 W,如果有,则填满 W 并减去 hands 中的两个 W,继续递归看 WRRBB。递归...

public int minSteps(String board,int[] hands){
	board=removeDuplicate3(board);
	if(board.length()==0){
		return 0;
	}
	char[] cs=board.toCharArray();
	int min=6;//因为The number of balls in your hand won't exceed 5
	int low=0;
	int high=1;
	int duplicate=1;
	while(low<cs.length){
		if(high>=cs.length||cs[high]!=cs[low]){
			int need=3-duplicate;
			if(hands[cs[low]-'A']>=need){
				hands[cs[low]-'A']-=need;
				String subStr="";
				if(high>=cs.length){
					subStr=board.substring(0,low);
				}
				else{
					 subStr=board.substring(0,low)+board.substring(high);
				}
				int step=need+minSteps(subStr, hands);
				if(step<min){
					min=step;
				}
				hands[cs[low]-'A']+=need;
			}
			low=high;
			high=low+1;
			duplicate=1;
		}
		else{
			high++;
			duplicate++;
		}
	}
	return min;
}

public String removeDuplicate3(String board){
	boolean isFind=true;
	String result="";
	while(isFind==true){
		isFind=false;
		if(board.length()<3){
			return board;
		}	
		result="";
		int low=0;
		int fast=1;
		int duplicate=1;
		//之所以不让fast<board.length()
		//是考虑到BYYBBRRB这个末尾有一个独立的字母的情况
		while(low<board.length()){
			while(fast<board.length()&&board.charAt(fast)==board.charAt(low)){
				fast++;
				duplicate++;
			}
			if(duplicate<3){
				for(int i=low;i<fast;i++){
					result+=board.charAt(i);
				}
			}
			else{
				isFind=true;
			}
			low=fast;
			fast=low+1;
			duplicate=1;			
		}
		board=result;
	}		
	return result;
}
需要注意的是 removeDuplicate3 方法中需要循环,是因为存在输入的 board 是 RRGGWYYYWWGGR 的情况。每次循环去除 一串 连续超过3次的字母,接下来继续循环。
大神也用的这个思路,但是怎么就看上去比我的要更加简洁呢?

It's just like a DFS or a Backtracking solution. word is poor, just look the code.

int MAXCOUNT = 6;   // the max balls you need will not exceed 6 since "The number of balls in your hand won't exceed 5"

public int findMinStep(String board, String hand) {
    int[] handCount = new int[26];
    for (int i = 0; i < hand.length(); ++i) ++handCount[hand.charAt(i) - 'A'];
    int rs = helper(board + "#", handCount);  // append a "#" to avoid special process while j==board.length, make the code shorter.
    return rs == MAXCOUNT ? -1 : rs;
}
private int helper(String s, int[] h) {
    s = removeConsecutive(s);     
    if (s.equals("#")) return 0;
    int  rs = MAXCOUNT, need = 0;
    for (int i = 0, j = 0 ; j < s.length(); ++j) {
        if (s.charAt(j) == s.charAt(i)) continue;
        need = 3 - (j - i);     //balls need to remove current consecutive balls.
        if (h[s.charAt(i) - 'A'] >= need) {
            h[s.charAt(i) - 'A'] -= need;
            rs = Math.min(rs, need + helper(s.substring(0, i) + s.substring(j), h));
            h[s.charAt(i) - 'A'] += need;
        }
        i = j;
    }
    return rs;
}
//remove consecutive balls longer than 3
private String removeConsecutive(String board) {
    for (int i = 0, j = 0; j < board.length(); ++j) {
        if (board.charAt(j) == board.charAt(i)) continue;
        if (j - i >= 3) return removeConsecutive(board.substring(0, i) + board.substring(j));
        else i = j;
    }
    return board;
}


0
0
查看评论

LeetCode 488 Zuma Game 解题报告

原文链接: http://hankerzheng.com/blog/Leetcode-Zuma-Game-Problem DescriptionLeetCode 488 Zuma Game You have a row of balls on the table, colored red(R), ...
  • hanzheng992
  • hanzheng992
  • 2017-01-26 11:10
  • 976

488. Zuma Game(leetcode DFS hard)

本题采用DFS求解,代码参考来自leetcode discussion, 地址如下 https://discuss.leetcode.com/topic/76681/simple-c-dfs-solution-using-list 不过我进行了相关的改进和说明。488. Zuma Game题目地...
  • qq_26437925
  • qq_26437925
  • 2017-02-24 11:48
  • 767

【LeetCode】488. Zuma Game

【LeetCode】488. Zuma Game
  • mtz5031
  • mtz5031
  • 2017-03-11 14:52
  • 681

[leetcode]488. Zuma Game

题目链接:https://leetcode.com/problems/zuma-game/description/ Think about Zuma Game. You have a row of balls on the table, colored red(R), yellow(Y),...
  • xiaocong1990
  • xiaocong1990
  • 2017-08-12 18:31
  • 144

Tsinghua OJ:祖玛(Zuma)

祖玛(Zuma) Description Let's play the game Zuma! There are a sequence of beads on a track at the right beginning. All the beads are colored but...
  • itismelzp
  • itismelzp
  • 2015-10-18 20:31
  • 2334

html5游戏开发之-----祖玛小游戏<原创>

/*祖玛小游戏相信很多人玩过, 今天我尝试在html5的画布上尝试用js写了一个小游戏, 开过过程的感悟是: 遇到一堆代码的时候,应该首先得出一个整体的框架 然后根据这个粗略的框架进行分块开发 即使刚开始的时候毫无头绪 也要即刻动手 昨天在看jQuery源码的时候 看...
  • qq_32207471
  • qq_32207471
  • 2016-04-24 10:45
  • 511

leetcode 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 i...
  • JackZhang_123
  • JackZhang_123
  • 2017-12-16 10:32
  • 59

Tsinghua MOOC 祖玛(Zuma)

描述 祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色。此后,你可以发射珠子到轨道上并加入原有序列中。一旦有三个或更多同色的珠子变成相邻,它们就会立即消失。这类消除现象可能会连锁式发生,其间你将暂时不能发射珠子。 开...
  • baidu_23318869
  • baidu_23318869
  • 2014-11-19 16:42
  • 2744

经典消除游戏——Unity 祖玛游戏

在Unity设计一个祖玛游戏
  • wuzhi3078
  • wuzhi3078
  • 2016-06-18 18:22
  • 673

Tsinghua OJ 数据结构编程作业:祖玛(Zuma)

Tsinghua OJ, 数据结构 ,邓俊辉,编程习题
  • xue_wenyuan
  • xue_wenyuan
  • 2016-03-15 18:13
  • 1955
    个人资料
    • 访问:280959次
    • 积分:5909
    • 等级:
    • 排名:第5176名
    • 原创:338篇
    • 转载:80篇
    • 译文:1篇
    • 评论:37条
    博客专栏
    leetcode

    文章:234篇

    阅读:44863
    文章分类