[Swift]LeetCode488. 祖玛游戏 | 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‘.


回忆一下祖玛游戏。现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W)。 现在你手里也有几个球。

每一次,你可以从手里的球选一个,然后把这个球插入到一串球中的某个位置上(包括最左端,最右端)。接着,如果有出现三个或者三个以上颜色相同的球相连的话,就把它们移除掉。重复这一步骤直到桌上所有的球都被移除。

找到插入并可以移除掉桌上所有球所需的最少的球数。如果不能移除桌上所有的球,输出 -1 。

示例:
输入: "WRRBBW", "RB"
输出: -1
解释: WRRBBW -> WRR[R]BBW -> WBBW -> WBB[B]W -> WW (翻译者标注:手上球已经用完,桌上还剩两个球无法消除,返回-1)

输入: "WWRRBBWW", "WRBRW"
输出: 2
解释: WWRRBBWW -> WWRR[R]BBWW -> WWBBWW -> WWBB[B]WW -> WWWW -> empty

输入:"G", "GGGGG"
输出: 2
解释: G -> G[G] -> GG[G] -> empty 

输入: "RBYYBBRRB", "YRBGB"
输出: 3
解释: RBYYBBRRB -> RBYY[Y]BBRRB -> RBBBRRB -> RRRB -> B -> B[B] -> BB[B] -> empty

标注:

  1. 你可以假设桌上一开始的球中,不会有三个及三个以上颜色相同且连着的球。
  2. 桌上的球不会超过20个,输入的数据中代表这些球的字符串的名字是 "board" 。
  3. 你手中的球不会超过5个,输入的数据中代表这些球的字符串的名字是 "hand"。
  4. 输入的两个字符串均为非空字符串,且只包含字符 ‘R‘,‘Y‘,‘B‘,‘G‘,‘W‘。


Runtime: 1004 ms

Memory Usage: 4 MB

 1 class Solution {
 2     func findMinStep(_ board: String, _ hand: String) -> Int {
 3         var res:Int = Int.max
 4         var s:Set<Character> = Set<Character>()
 5         for i in 0..<hand.count
 6         {
 7             if s.contains(hand[i]) {continue}
 8             s.insert(hand[i])
 9             for j in 0..<board.count
10             {
11                 if board[j] != hand[i] {continue}
12                 var newBoard = board
13                 var newHand = hand
14                 let index1 = newBoard.index(newBoard.startIndex, offsetBy: j)
15                 newBoard.insert(hand[i],at:index1)
16                 newBoard = removeConsecutive(newBoard)
17                 if newBoard.count == 0 {return 1}
18                 let index2 = newHand.index(newHand.startIndex, offsetBy: i)
19                 newHand.remove(at:index2)
20                 var cnt:Int = findMinStep(newBoard, newHand)
21                 if cnt != -1
22                 {
23                     res = min(res, cnt + 1)
24                 }
25             }
26         }
27         return res == Int.max ? -1 : res
28     }
29
30     func removeConsecutive(_ board:String) -> String
31     {
32         var j:Int = 0
33         for i in 0...board.count
34         {
35             if i < board.count && board[i] == board[j] {continue}
36             if i - j >= 3 {return removeConsecutive(board.subString(0, j) + board.subString(i))}
37             else{j = i}
38         }
39         return board
40     }
41 }
42
43 extension String {
44     //subscript函数可以检索数组中的值
45     //直接按照索引方式截取指定索引的字符
46     subscript (_ i: Int) -> Character {
47         //读取字符
48         get {return self[index(startIndex, offsetBy: i)]}
49     }
50
51     // 截取字符串:指定索引和字符数
52     // - begin: 开始截取处索引
53     // - count: 截取的字符数量
54     func subString(_ begin:Int,_ count:Int) -> String {
55         let start = self.index(self.startIndex, offsetBy: max(0, begin))
56         let end = self.index(self.startIndex, offsetBy:  min(self.count, begin + count))
57         return String(self[start..<end])
58     }
59
60     // 截取字符串:从index到结束处
61     // - Parameter index: 开始索引
62     // - Returns: 子字符串
63     func subString(_ index: Int) -> String {
64         let theIndex = self.index(self.endIndex, offsetBy: index - self.count)
65         return String(self[theIndex..<endIndex])
66     }
67 }

原文地址:https://www.cnblogs.com/strengthen/p/10348594.html

时间: 2024-10-28 22:05:46

[Swift]LeetCode488. 祖玛游戏 | Zuma Game的相关文章

Swift版iOS游戏框架Sprite Kit基础教程下册

Swift版iOS游戏框架Sprite Kit基础教程下册 试读下载地址:http://pan.baidu.com/s/1qWBdV0C 介绍:本教程是国内唯一的Swift版的Spritekit教程.本教程基于Xcode 6.1+iOS 8.1开发环境,采用Swift语言,详细讲解Sprite Kit游戏开发的各种知识,帮助读者尽快iOS游戏开发的技能. 目录 第7章  音频和视频 1 7.1  背景音乐 1 7.1.1  添加背景音乐 1 7.1.2  控制背景音乐 5 7.1.3  设置音乐

LeetCode 488.祖玛游戏

回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串球中的某个位置上(包括最左端,最右端).接着,如果有出现三个或者三个以上颜色相同的球相连的话,就把它们移除掉.重复这一步骤直到桌上所有的球都被移除. 找到插入并可以移除掉桌上所有球所需的最少的球数.如果不能移除桌上所有的球,输出 -1 . 示例:输入: "WRRBBW", "RB"

T4310 祖玛游戏

题目描述 祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干 个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到 轨道上并加入原有序列中.一旦有三个或更多同色的珠子变成相邻,它们就会立 即消失.这类消除现象可能会连锁式发生,其间你将暂时不能发射珠子. 开发商最近准备为玩家写一个游戏过程的回放工具. 他们已经在游戏内完成 了过程记录的功能,而回放功能的实现则委托你来完成. 游戏过程的记录中,首先是轨道上初始的珠子序列,然后是玩家接下来所做 的一系列操作.你的任务

swift 拼图小游戏

根据这位朋友的拼图小游戏改编 http://tangchaolizi.blog.51cto.com/3126463/1571616 改编主要地方是: 原本着我仁兄的代码时支持拖动小图块来移动的,我参照之前自己java当初写的,其实不需要拖动,因为只有一个空出来地方,那么通过点击事件,接受到点击事件的小图只能向一个方向移动或者不能移动. 通过对这个游戏的学习,我也算是第一次用swift写一个有用的代码,大家互相学习. 代码下载位置 版权声明:本文为博主原创文章,未经博主允许不得转载.

[Swift]LeetCode292. Nim游戏 | Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove th

[Swift]LeetCode877. 石子游戏 | Stone Game

Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones.  The total number of stones

[Swift]LeetCode293. 翻转游戏 $ Flip Game

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip twoconsecutive "++" into "--". The game ends when a person can no longer

iOS开发 Swift开发数独游戏(二)数独题目的生成

一.Plist文件结构设计 由于要预先生成数独题目的文件,我自然而然想到用plist存取. 我用Xcode建了几个plist文件来熟悉这种文件使用的结构后设计了如下结构: 为区分难度(后来了解到挖空数与难度其实不一定相对应),我笼统的以挖空数分类,每一个分类下存储这一挖空数对应的数独题目与解. 具体来说,root使用Dictionary类型,下面的关键字以"D"开头,后面为挖空的数目,每个"DXX"为Array类型,内容为不同的数独题目与解,每个数独题是Dictio

[Swift]LeetCode45. 跳跃游戏 II | Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps