uva 10404 - Bachet's Game(DP)

1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1345

2、题目大意:

给n个石子,有m种取法,每次可以取m堆中的某一堆,Stan先取,Ollie再取,谁取到最后一个石子,谁获胜,最后输出获胜者即可

乍一看挺简单,不过还是没想出来怎么做,看网上的代码

dp[i]表示当前剩余i个石子时Stan的获胜状态,1表示获胜,0表示输了,

状态转移方程:if(i>=stone[j] && dp[i-stone[j]]==0) dp[i]=1;

3、题目:

Problem B: Bachet's Game

Bachet's game is probably known to all but probably not by this name. Initially there are n stones on the table. There are two players Stan and Ollie, who move alternately.  Stan always starts. The legal moves consist in removing at least one but not more thank stones from the table.  The winner is the one to take the last stone.

Here we consider a variation of this game.  The number of stones that can be removed in a single move must be a member of a certain set of m numbers.  Among the m numbers there is always 1 and thus the game never stalls.

Input

The input consists of a number of lines.  Each line describes one game by a sequence of positive numbers. The first number isn <= 1000000 the number of stones on the table; the second number is m <= 10 giving the number of numbers that follow; the last m numbers on the line specify how many stones can be removed from the table in a single move.

Input

For each line of input, output one line saying either Stan winsor Ollie wins assuming that both of them play perfectly.

Sample input

20 3 1 3 8
21 3 1 3 8
22 3 1 3 8
23 3 1 3 8
1000000 10 1 23 38 11 7 5 4 8 3 13
999996 10 1 23 38 11 7 5 4 8 3 13

Output for sample input

Stan wins
Stan wins
Ollie wins
Stan wins
Stan wins
Ollie wins

Problem Setter: Piotr Rudnicki

 

4、
#include<stdio.h>
#include<string.h>
int stone[15];
int dp[1000005];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<m;i++)
        scanf("%d",&stone[i]);
        memset(dp,0,sizeof(dp));
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(i>=stone[j] && dp[i-stone[j]]==0)
                {
                    dp[i]=1;
                    break;
                }
            }
        }
        if(dp[n]==1)
        printf("Stan wins\n");
        else
        printf("Ollie wins\n");
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值