您的位置:首页 > 其它

hdu4155 The Game of 31

2013-10-02 23:04 316 查看

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


[align=left]Problem Description[/align]
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

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

356656
35665
3566
111126666
552525

[align=left]Sample Output[/align]

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: