hdu4155 The Game of 31

The Game of 31

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 480 Accepted Submission(s): 234


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
壮态很少,暴搜!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
int a[7];char str[300];
int dfs(int x){
    int i;
    if(x>31)return 1;
    for(i=1;i<=6;i++){
        if(a[i]<4){
            a[i]++;
            if(dfs(x+i)==0){
                a[i]--;
                return 1;
            }
            a[i]--;
        }
    }
    return 0;
}
int main()
{
    int i,ans;
    while(scanf("%s",str)!=EOF){
        printf("%s ",str);
        mem(a,0);
        for(i=0,ans=0;str[i]!='\0';i++){
            ans+=str[i]-'0';
            a[str[i]-'0']++;
            if(ans>31)break;
        }
        if(ans>31){
            if(i&1)printf("A\n");
            else printf("B\n");
        }
        else {
            int re=dfs(ans);
            if((i&1))
            re=re?0:1;
            if(re)printf("A\n");
            else printf("B\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值