hdu 4155

The Game of 31

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


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
搜索博弈
 数据不大,直接搜索一下
 1 #include<iostream>
 2 #include<string>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<queue>
 6 #include<stack>
 7 #include<set>
 8 #include<algorithm>
 9 #include<cstring>
10 #include<stdlib.h>
11 #include<math.h>
12 #include<map>
13 using namespace std;
14 #define ll long long
15 int num[10];
16 int dfs(int n){
17     for(int i=1;i<=6&&n>=i;i++){
18         if(num[i]<4){
19             num[i]++;
20             if(!dfs(n-i)){
21                 num[i]--;
22                 return 1;
23             }
24             num[i]--;
25         }
26     }
27     return 0;
28 }
29 int main(){
30      #ifndef ONLINE_JUDGE
31             freopen("input.txt","r" ,stdin);
32         #endif // ONLINE_JUDGE
33     string a;
34     while(cin>>a){
35         int sum=0;
36         memset(num,0,sizeof(num));
37         for(int i=0;i<a.size();i++) num[a[i]-'0' ]++,sum+=a[i]-'0';
38         cout<<a<<" ";
39         if(dfs(31-sum)) {
40             if(a.size()&1) cout<<"B";
41             else cout<<"A";
42         }
43         else {
44             if(a.size()&1) cout<<"A";
45             else cout<<"B";
46         }
47         cout<<endl;
48     }
49 }
原文地址:https://www.cnblogs.com/ainixu1314/p/3934729.html