Hackerrank石头游戏如果条件不确定,我如何找到答案

问题描述

https://www.hackerrank.com/challenges/game-of-stones-1/problem

石头游戏。

两个叫P1P2的玩家正在玩以石头开头的游戏。玩家1始终是第一位玩家,两位玩家交替轮流移动。游戏的规则如下:

在一个步骤中,玩家可以从游戏板上移除2、3或5块石头。 如果玩家无法采取行动,则该玩家将输掉比赛。 给定开始的石头数目,找到并打印获胜者的名字。 P1被命名为First,而P2被命名为Second。每个玩家的比赛都达到了最佳状态,这意味着如果存在获胜的举动,他们将不会采取任何行动,导致他们输掉比赛。

例如,如果n = 4P1可以采取以下行动:

P1移除2个石头,剩下2个。P2随后移除2个石头并获胜。 P1除去3块石头,剩下1块。P2无法移动并丢失。 P1将赢得第二局并赢得比赛。

功能描述

在下面的编辑器中完成gameOfStones函数。它应该返回第一个或第二个字符串。

gameOfStones具有以下参数:

n:代表宝石起始数目的整数

输入格式

第一行包含一个整数,即测试用例的数量。 接下来的每一行都包含一个整数,即测试用例中的结石数量。

约束

1

输出格式

在每个测试用例的新行中,如果第一个玩家是获胜者,则打印First。否则,打印第二。

我的问题

在此文档的链接中,玩家每回合可以拿2、3或5块石头。

但是,如果每种情况下结石的数量和条件的数量不同,我该如何编写代码?

例如。情况1,玩家可以拿2、3或5石头,情况2,玩家可以拿2、4、7、9石头。

和代码将通过两种情况。

输入 情况1:

3  //total conditions of stones can take
2 3 5 //player can take 2,3 or 5 stones
8 // Number of cases of number of starting stones
1
2
3
4
5
6
7
10

情况2:

4  //total conditions of stones can take
2 3 7 9 //players can take 2,3,7 or 9 stones
5 // Number of cases of number of starting stones
5
6
7
10
15

并且代码将通过两种情况。我应该如何编写满足这种情况的编码?

解决方法

我在Swift中为您的新问题写了解决方案。如果您不熟悉它,希望它与您使用过的有用语言足够相似。

这是解决一般情况的方法。

// This is an internal function that also takes a dictionary of results so that
// it can remember solutions it has already found
func game(n: Int,conditions: [Int],result: inout [Int : String]) -> String {

    // Have we seen this answer before?  If so,just return it
    if let answer = result[n] {
        return answer
    }

    if n < conditions.min()! {
        // I can't move because the number of stones left is fewer than
        // I'm allowed to take
        result[n] = "Second" // to speed up the solution,remember this result
        return "Second"
    } else if conditions.contains(n) {
        // I can take all of the stones,so I win
        result[n] = "First"  // to speed up the solution,remember this result
        return "First"
    } else {
        // Try taking each of the stones I'm allowed to take,and see
        // if that causes my opponent to lose
        for take in conditions {
            let leave = n - take

            // If the number of stones I leave causes the opponent to lose,I win
            if leave > 0 && game(n: leave,conditions: conditions,result: &result) == "Second" {
                result[n] = "First" // to speed up the solution,remember this result
                return "First"
            }
        }
    }
    
    // No way for me to win,so I come in second.
    result[n] = "Second"  // to speed up the solution,remember this result
    return "Second"
}

// Generate a dictionary to store already generated answers,and call the
// internal recursive routine
func gameOfStones(n: Int,conditions: [Int]) -> String {
    var result = [Int : String]()
    
    return game(n: n,result: &result)
}

print(gameOfStones(n: 4,conditions: [2,3,5]))   // "First"
print(gameOfStones(n: 6,conditions: [3,7,13]))  // "Second"

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...