博弈——Digital Deletions

Digital deletions is a two-player game. The rule of the game is as following. 

Begin by writing down a string of digits (numbers) that's as long or as short as you like. The digits can be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and appear in any combinations that you like. You don't have to use them all. Here is an example: 



On a turn a player may either: 
Change any one of the digits to a value less than the number that it is. (No negative numbers are allowed.) For example, you could change a 5 into a 4, 3, 2, 1, or 0. 
Erase a zero and all the digits to the right of it. 


The player who removes the last digit wins. 


The game that begins with the string of numbers above could proceed like this: 



Now, given a initial string, try to determine can the first player win if the two players play optimally both. 
Input
The input consists of several test cases. For each case, there is a string in one line. 

The length of string will be in the range of [1,6]. The string contains only digit characters. 

Proceed to the end of file. 
Output
Output Yes in a line if the first player can win the game, otherwise output No. 
Sample Input
0
00
1
20
Sample Output
Yes
Yes
No
No


题意:给一串数字,有两个玩家,每回合可以将某个数字改成比它小的数(所有数字包括改之后都为非负整数),或者将某个0和0的右边都消去。能执行最后一次移动的玩家为赢,求最先移动的人是否能赢。

思路:

因为范围较小可以先将每个数字的胜负态sg置0表示为输,然后sg[0]=1,表示为0的时候会赢,然后往后找必败态,找到后进行筛选将必败态能转移到的数值置1表示必胜。

例如:

1为必败态而1能转移到2~9和10~109999,所以可以将这些数值置1,然后一直找下去,这样就可以事先打好表。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000000;
int sg[maxn];
int a[10];
int qu_len(int x)     //求数值的长度
{
    int cnt=1;
    while(x/10)
    {
        cnt++;
        x/=10;
    }
    return cnt;
}
void work(int x)    //对x这种必败态进行转移
{
    int len=qu_len(x);
    for(int i=1; i<=len; i++)
    {
        int m=x;
        int pos=1;
        for(int j=1; j<i; j++)
            pos*=10;
        int t=(m/pos)%10;
        for(int j=t; j<9; j++)
        {
            m+=pos;
            sg[m]=1;
        }
    }
    int pos=1;
    int m=x;
    for(int i=len; i<6; i++)
    {
        m*=10;
        for(int j=0; j<pos; j++)
            sg[m+j]=1;
        pos*=10;
    }
}
void init()     //打表
{
    memset(sg, 0, sizeof(sg));
    sg[0]=1;
    for(int i=1; i<maxn; i++)
        if(!sg[i])
            work(i);
}
int main()
{
    init();
    char a[10];
    while(~scanf("%s", a))
    {
        if(a[0]=='0')
        {
            printf("Yes\n");
            continue;
        }
        int s=a[0]-'0';
        int l=strlen(a);
        for(int i=1; i<l; i++)
        {
            s*=10;
            s+=a[i]-'0';
        }
        if(sg[s])
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值