算法设计与应用基础:第十四周(1)

来源:互联网 发布:免费分析软件 编辑:程序博客网 时间:2024/05/22 06:24

464. Can I Win

DescriptionHintsSubmissionsSolutions
  • Total Accepted: 9216
  • Total Submissions: 38595
  • Difficulty: Medium
  • Contributors:taylorty

In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins. 

What if we change the game so that players cannot re-use integers? 

For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.

Given an integer maxChoosableInteger and another integer desiredTotal, determine if the first player to move can force a win, assuming both players play optimally. 

You can always assume that maxChoosableInteger will not be larger than 20 and desiredTotal will not be larger than 300.


解题报告:题意大概就是两个人玩游戏,从1,2·····n中随意拿数字(不能拿之前拿过的,也就是每个数字只能拿一次),两个人拿的数字总和记为ans,谁先拿数字使得ans>=tar,谁就赢了,该题就是问对于先拿的人,给定n,tar,是否存在必胜策略(也就是说,是否存在某种方法,使得第一个人拿完一次以后,无论第二个人如何拿,第一个人都有对应方法使得自己必胜)。

该题没有什么数学思维,查看discuss也是直接dfs加一个map提速(就是遍历所有可能的情况,查看是否存在一种可能第一个人拿完一张,另一个人无论如何拿,都是第一个人赢,对应于n的数字的全排列,取定一个元素后,剩余n-1个元素的全排列都会保证第一个人先到达tar),代码如下

class Solution {public:   private:    int maxn;    map<int, bool> m;//保存对应的某一个串状态,是否有必胜策略,所以最后返回m[0]就可以了public:    bool canIWin(int maxChoosableInteger, int desiredTotal) {        maxn = maxChoosableInteger;        if(maxn >= desiredTotal) return true;//如果第一次有比目标数字大的,那第一个人不傻的话肯定拿了就赢了        if((1 + maxn) * maxn / 2 < desiredTotal) return false;//如果所有数字加起来都没有目标数字大,那肯定无论如何没有必胜策略        return canWin(desiredTotal, 0);//最精妙的一点,如同上课老师所讲的,用一个串来保存一个集合(这里对于int32位的低n位保存n个元素是否被取过的状态,某一位是1表示被取过,是0没有,初始都为0,所以int也是0    }    bool canWin(int target, int visited) {        if(m.find(visited) != m.end()) return m[visited];        for(int i = 1; i <= maxn; i++) //深度优先搜索,对于每一个可以拿的数字,看它是不是必胜策略
 {            int mask = (1 << i);//得到第i位的二进制表示10····(i-1个0)            if((mask & visited) == 0 && (i >= target || canWin(target - i, mask | visited) == false))            {                m[visited] = true;                return true;            }        }        m[visited] = false;        return false;    }};


原创粉丝点击