[POJ2068]Nim解题报告

本文介绍了一种传统游戏Nim的策略玩法,并通过记忆化搜索算法实现了一个判断当前玩家是否拥有获胜策略的程序。该算法适用于团队游戏场景,考虑了不同玩家移除石头数量的限制。

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! 

 

  记忆化搜索就可以过了

  感觉记忆化搜索在某些题目里比DP要好用

  比如这道题,用记忆化搜索时间复杂度不变

  但是如果用DP确实不知道按照什么顺序来搞...

 

program poj2068;
var n,s,i:longint;
    f:array[-1..25,-1..10010]of longint;
    a:array[-1..25]of longint;

procedure dfs(i,j:longint);
var k:longint;
begin
        if f[i,j]<>-1 then exit;
        f[i,j]:=0;
        for k:=1 to a[i] do if k<=j then
        begin
                dfs(i mod n+1,j-k);
                if f[i mod n+1,j-k] = 0 then
                begin
                        f[i,j]:=1;
                        break;
                end;
        end;
end;

begin
    read(n);
        while n<>0 do
    begin
        read(s);
        for i:=1 to 2*n do read(a[i]);
        readln;n:=n << 1;
        fillchar(f,sizeof(f),255);
                for i:=1 to n do f[i,0]:=1;
                dfs(1,s);
                writeln(f[1,s]);
                read(n);
    end;
end.

 

转载于:https://www.cnblogs.com/mjy0724/p/4466787.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值