hdu 4155 The Game of 31

本文介绍了一个两人轮流出牌的游戏“31”的玩法及获胜策略。游戏中使用四套1至6的扑克牌,目标是在不超过31的情况下阻止对手完成出牌。文章提供了一个示例代码,展示了如何通过递归搜索确定最优策略并预测胜者。

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

题目链接:点这里

Problem Description

The game of 31 was a favourite of con artists who rode the railroads in days of yore. The game is played with a deck of 24 cards: four labelled each of 1, 2, 3, 4, 5, 6. The cards in the deck are visible to both players, who alternately withdraw one card from the deck and place it on a pile. The object of the game is to be the last player to lay a card such that the sum of the cards in the pile does not exceed 31. Your task is to determine the eventual winner of a partially played game, assuming each player plays the remainder of the game using a perfect strategy.
For example, in the following game player B wins:
Player A plays 3
Player B plays 5
Player A plays 6
Player B plays 6
Player A plays 5
Player B plays 6

Input

The input will consist of several lines; each line consists of a sequence of zero or more digits representing a partially completed game. The first digit is player A's move; the second player B's move; and so on. You are to complete the game using a perfect strategy for both players and to determine who wins.

Output

For each game, print a line consisting of the input, followed by a space, followed by A or B to indicate the eventual winner of the game.

Sample Input

356656
35665
3566
111126666
552525

Sample Output

356656 B
35665 B
3566 A
111126666 A
552525 A

【题意】

桌子上本来有4*(1,2,3,4,5,6) = 24张扑克牌。两个人轮流取走其中的一张,将所有的点数加起来。现在游戏已经进行了一部分了,问你现在游戏继续下去之后,问你谁能先将点数加到超过31,首先超过的算败。

【分析】

首先得判断一下当前状态是否已经超过了31。按照结果对应输出就行了。如果没有就直接暴力搜索就行了。

【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<sstream>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS_1(a) memset(a,-1,sizeof(a))
#define MSinf(a) memset(a,0x3f,sizeof(a))
#define sin1(a) scanf("%d",&(a))
#define sin2(a,b) scanf("%d%d",&(a),&(b))
#define sll1(a) scanf("%lld",&(a))
#define sll2(a,b) scanf("%lld%lld",&(a),&(b))
#define sdo1(a) scanf("%lf",&(a))
#define sdo2(a,b) scanf("%lf%lf",&(a),&(b))
#define inf 0x3f3f3f3f
//#define lson i<<1,l,mid
//#define rson ((i<<1)|1),mid+1,r
#define uint unsigned int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define MK make_pair
#define ll long long
template<typename T>
void read1(T &m) {
    T x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9') {
        x=x*10+ch-'0';
        ch=getchar();
    }
    m = x*f;
}
template<typename T>
void read2(T &a,T &b) {
    read1(a);
    read1(b);
}
template<typename T>
void read3(T &a,T &b,T &c) {
    read1(a);
    read1(b);
    read1(c);
}
template<typename T>
void out(T a) {
    if(a>9) out(a/10);
    putchar(a%10+'0');
}
template<typename T>
void outn(T a) {
    if(a>9) out(a/10);
    putchar(a%10+'0');
    puts("");
}
///------------------------------------------------------------------------------------
char s[50];
int num[10];
int getsg(int a)
{
    if(a>31) return 0;
   rep1(i,1,6)
   if(num[i]&&a+i<=31)
   {
       num[i]--;
       if(getsg(a+i)==0)
       {
           num[i]++;
           return 1;
       }
       num[i]++;
   }
   return 0;
}
int main() {
//    freopen("in.txt","r",stdin);
    while(scanf("%s",s)!=EOF)
    {
        printf("%s ",s);
        fill(num,num+10,4);
        int ans = 0;
        int len=strlen(s);
        rep0(i,0,len)
        {
            int t = s[i] - '0';
            num[t]--;
            ans+=t;
        }
        if(getsg(ans)) puts(len&1?"B":"A");
        else puts(len&1?"A":"B");
        /*
        后来还看到了一个简化版本,写的十分飘逸
        if(getsg(ans)) printf("%c\n",'A'+(len&1));
        else printf("%c\n",'B'-(len&1));
        */
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值