zoukankan      html  css  js  c++  java
  • poj 2068 Nim(博弈树)

    Nim
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 1501   Accepted: 845

    Description

    Let's play a traditional game Nim. You and I are seated across a table and we have a hundred stones on the table (we know the number of stones exactly). We play in turn and at each turn, you or I can remove on to four stones from the heap. You play first and the one who removed the last stone loses.
    In this game, you have a winning strategy. To see this, you first remove four stones and leave 96 stones. No matter how I play, I will end up with leaving 92 - 95 stones. Then you will in turn leave 91 stones for me (verify this is always possible). This way, you can always leave 5k+1 stones for me and finally I get the last stone, sigh. If we initially had 101 stones, on the other hand, I have a winning strategy and you are doomed to lose.

    Let's generalize the game a little bit. First, let's make it a team game. Each team has n players and the 2n players are seated around the table, with each player having opponents at both sides. Turn around the table so the two teams play alternately. Second, let's vary the maximum number of stones each player can take. That is, each player has his/her own maximum number of stones he/she can take at each turn (The minimum is always one). So the game is asymmetric and may even be unfair.

    In general, when played between two teams of experts, the outcome of a game is completely determined by the initial number of stones and the maximum number of stones each player can take at each turn. In other words, either team has a winning strategy.

    You are the head-coach of a team. In each game, the umpire shows both teams the initial number of stones and the maximum number of stones each player can take at each turn. Your team plays first. Your job is, given those numbers, to instantaneously judge whether your team has a winning strategy.

    Incidentally, there is a rumor that Captain Future and her officers of Hakodate-maru love this game, and they are killing their time playing it during their missions. You wonder where the stones are? Well, they do not have stones but do have plenty of balls in the fuel containers!

    Input

    The input is a sequence of lines, followed by the last line containing a zero. Each line except the last is a sequence of integers and has the following format.

    n S M1 M2 . . . M2n

    where n is the number of players in a team, S the initial number of stones, and Mi the maximum number of stones ith player can take. 1st, 3rd, 5th, ... players are your team's players and 2nd, 4th, 6th, ... the opponents. Numbers are separated by a single space character. You may assume 1 <= n <= 10, 1 <= Mi <= 16, and 1 <= S < 2^13.

    Output

    The output should consist of lines each containing either a one, meaning your team has a winning strategy, or a zero otherwise.

    Sample Input

    1 101 4 4
    1 100 4 4
    3 97 8 7 6 5 4 3
    0

    Sample Output

    0
    1
    1
    

    Source

    【思路】

           博弈

           构造博弈树,一个局面必胜当且仅当后继局面有至少一个必败局面,一个局面必败当且仅当后继局面都为必胜局面。

           记忆化搜索即可。

    【代码】

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
     5 using namespace std;
     6 
     7 const int N =  1e4;
     8 
     9 int f[21][N],a[21];
    10 int n,m;
    11 
    12 int dfs(int r,int tot) {
    13     if(r==2*n+1) r=1;
    14     int &ans=f[r][tot];
    15     if(ans!=-1) return ans;
    16     if(tot==1) return ans=0;
    17     if(tot<=a[r]) return ans=1;
    18     FOR(i,1,a[r]) 
    19         if(!dfs(r+1,tot-i)) return ans=1;
    20     return ans=0;
    21 }
    22 
    23 int main() {
    24     while(scanf("%d",&n)==1 && n) {
    25         scanf("%d",&m);
    26         FOR(i,1,2*n) scanf("%d",&a[i]); 
    27         memset(f,-1,sizeof(f));
    28         if(dfs(1,m)) puts("1");
    29         else puts("0");
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    如何从程序集中加载及卸载插件(下)
    Castle AOP 系列(四):实现一个简单的基于上下文调用的权限校验机制
    Castle AOP 系列(一):对类方法调用的拦截
    Mussel使用系列(六):分布式调用的牛刀小试
    新发现XmlNode中变态的地方
    我们发现你在XX邮箱的账号密码与其他网站被盗账号密码一致 请立即更改密码。
    html5 css3 新特性一览表
    [android] Http Post 请求
    [vs 使用技巧] VS2013显示行数 行号
    ORACLE数据库存储使用情况查询命令
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5172091.html
Copyright © 2011-2022 走看看