hdu 4155 The Game of 31

题目链接:点这里

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;
}
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页