HDU 1404 Digital Deletions [SG]【博弈】

本文介绍了一款名为“数字删除”的两人对战游戏,并详细解析了游戏策略。通过使用SG函数,文章阐述了如何确定先手玩家是否能在双方都采取最优策略的情况下赢得比赛。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1404
————————————————————————————————————————————
Digital Deletions

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2891 Accepted Submission(s): 1052

Problem Description
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

————————————————————————————————————————————
题目大意:

给你一个长度不超过6的有数字组成的字符串,两个人分别操作,最后一次操作的人赢,问先手能不能赢.
有两种操作:
将任意一个不为0的数字变成小于它的数字
将任意一个0开始及其右面的左右数字删除.


很容易想到要用SG函数来求解,且第一个数字为0的先手必胜

但是这种时候用mex操作来求解SG函数实在太过麻烦,而且其中对于有前置0的状态很不好表示。

所以这里求解SG函数的时候用定义来求:
SG[]=0 表示必败 SG[]=1 表示必胜

能一步到达必败态的状态一定是必胜态.

所以我们从必败态出发,很容易表示其上一步状态,且不会出现有前置0的情况,

有SG[1]=0;
所以能够确保能够到达所有的状态在[1,1000000)

附本题代码
————————————————————————————————————————————

int sg[1000001];
int h[70],my[111],len;

int pow10(int x){
    if(x==0) return 1;
    if(x==1) return 10;
    if(x==2) return 100;
    if(x==3) return 1000;
    if(x==4) return 10000;
    if(x==5) return 100000;
}

void findsg(int x){
    int tem = x;len=0;
    for(;tem;tem/=10)      my[len++]=tem%10;

    for(int j=0;j<len;j++)
        for(int k=1;k+my[j]<10;k++)
            sg[x+pow10(j)*k]=1;

    int k=1;
    for(x*=10;x<1000000;x*=10) {
        for(int i=0;i<k;i++) sg[x+i]=1;
        k*=10;
    }

    return ;
}

void init(){
    memset(sg,0,sizeof(sg));
    sg[0]=1;
    for(int i=1;i<1000000;i++)if(!sg[i])
        findsg(i);
}

char s[10];
int main(){
    init();
    while(~scanf("%s",s)){
        if(s[0]=='0') {
            puts("Yes");
            continue;
        }

        int x=0;
        for(int i=0;s[i];i++) x=(x<<3)+(x<<1)+s[i]-'0';

        if(sg[x]) puts("Yes");
        else      puts("No");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值