Description
Axel and Birgit like to play a card game in which they build a house of cards, gaining (or losing) credits as they add cards to the house. Since they both have very steady hands, the house of cards never collapses. They use half a deck of standard playing cards. A standard deck has four suits, two are red and two are black. Axel and Birgit use only two suits, one red, one black. Each suit has 13 ranks. We use the notation 1R, 2R, ..., 13R, 1B, 2B, ..., 13B to denote ranks and colors.
The players begin by selecting a subset of the cards, usually all cards of rank up to some maximum value M. After shuffling the modified deck, they take eight cards from the top of the deck and place them consecutively from left to right to form four ``peaks." For instance, if M = 13 and if the first ten cards (from 26) are:
6B 3R 5B 2B 1B 5R 13R 7B 11R 1R ...
then the game starts off with four peaks and three valleys as shown in Figure 7.
The remaining cards are placed face up, in a single row.
Each player is identified with a color, red or black. Birgit is always black and Axel is always red. The color of the first card used for the peaks and valleys determines which player has the first turn. For the example in Figure 7, Birgit has the first turn since the first card is 6B.
Players alternate taking turns. A turn consists of removing the card from the front of the row of cards and then doing one of the following:
- Holding the card until the next turn (this is a ``held card").
- Covering the valley between two peaks with the drawn card or the held card, forming a ``floor". The remaining card, if any, is held.
- Placing two cards over a floor, forming a peak (one of the cards must be a ``held'' card).
Not all options are always available. At most one card may be held at any time, so the first option is possible only if the player is not already holding a card.
Since the cards in the row are face up, both players know beforehand the order in which the cards are drawn.
If the player forms a downward-pointing triangle by adding a floor, or an upward-pointing triangle by adding a peak, then the scores are updated as follows. The sum of the ranks of the three cards in the triangle is added to the score of the player whose color is the same as the majority of cards in the triangle. If no triangle is formed during a play, both scores remain unchanged.
In the example from Figure 7, if Birgit places her card (11R) on the middle valley, she gains 14 points. If she places her card on the left valley, Axel gains 19 points. If she places her card on the right valley, Axel gains 29 points.
If no more cards remain to be drawn at the end of a turn, the game is over. If either player holds a card at this time, the rank of that card is added to (subtracted from) that player's score if the color of the card is the same as (different from) that player's color.
When the game is over, the player with the lower score pays a number of Swedish Kronor (Kronor is the plural of Krona) equal to the difference between the two scores to the other player. In case of a tie there is no pay out.
You must write a program that takes a description of a shuffled deck and one of the players' names and find the highest amount that player can win (or the player's minimum loss), assuming that the other player always makes the best possible moves.
Input
The input file contains multiple test cases representing different games. Each test case consists of a name (either `Axel' or `Birgit'), followed by a maximum rank M ( 5M13), followed by the rank and color of the 2M cards in the deck in their shuffled order. Every combination of rank (from 1 through M) and color will appear once in this list. The first eight cards form the initial row of peaks from left to right in the order drawn, and the remaining items show the order of the rest of the cards.
The final test case is followed by a line containing the word `End'.
Output
For each test case, print the test case number (starting with 1), the name of the player for the test case, and the amount that the player wins or loses. If there is a tie, indicate this instead of giving an amount. Follow the sample output format.
题目大意:略。
思路:剪枝搜索。
代码(1895MS):
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 8 const int UP = 0; 9 const int FLOOR = 1; 10 const int DOWN = 2; 11 const int INF = 0x7fffffff; 12 13 int deck[33]; 14 char s[10], c; 15 int n; 16 17 inline int abs(int x) {int y = x >> 31; return (x + y) ^ y;} 18 19 inline int getScore(int a, int b, int c) { 20 int ret = abs(a) + abs(b) + abs(c); 21 int sgn = (a > 0) + (b > 0) + (c > 0); 22 if(sgn > 1) return ret; 23 else return -ret; 24 } 25 26 struct State { 27 int card[8], type[8]; 28 int hold[2]; 29 int pos, score; 30 31 State() { 32 for(int i = 0; i < 8; ++i) { 33 card[i] = deck[i]; 34 type[i] = (i % 2 == 0) ? UP : DOWN; 35 } 36 hold[0] = hold[1] = score = 0; 37 pos = 8; 38 } 39 40 bool isFinal() { 41 if(pos == 2 * n) { 42 score += hold[0] + hold[1]; 43 hold[0] = hold[1] = 0; 44 return true; 45 } 46 return false; 47 } 48 49 State child() const { 50 State s; 51 memcpy(&s, this, sizeof(s)); 52 s.pos = pos + 1; 53 return s; 54 } 55 56 void expand(int player, vector<State> &ret) const { 57 int &cur = deck[pos]; 58 if(hold[player] == 0) { 59 State s = child(); 60 s.hold[player] = cur; 61 ret.push_back(s); 62 } 63 for(int i = 0; i < 7; ++i) if(type[i] == DOWN && type[i + 1] == UP) { 64 State s = child(); 65 s.score += getScore(card[i], card[i + 1], cur); 66 s.type[i] = s.type[i + 1] = FLOOR; 67 s.card[i] = s.card[i + 1] = cur; 68 ret.push_back(s); 69 if(hold[player] != 0) { 70 State s = child(); 71 s.score += getScore(card[i], card[i + 1], hold[player]); 72 s.type[i] = s.type[i + 1] = FLOOR; 73 s.card[i] = s.card[i + 1] = hold[player]; 74 s.hold[player] = cur; 75 ret.push_back(s); 76 } 77 } 78 if(hold[player] != 0) { 79 for(int i = 0; i < 7; ++i) if(type[i] == FLOOR && type[i + 1] == FLOOR && card[i] == card[i + 1]) { 80 State s = child(); 81 s.score += getScore(card[i], hold[player], cur); 82 s.type[i] = UP; s.type[i + 1] = DOWN; 83 s.card[i] = cur; s.card[i + 1] = hold[player]; s.hold[player] = 0; 84 ret.push_back(s); 85 swap(s.card[i], s.card[i + 1]); 86 ret.push_back(s); 87 } 88 } 89 } 90 }; 91 92 int alphabeta(State &s, int player, int alpha, int beta) { 93 if(s.isFinal()) return s.score; 94 vector<State> children; 95 s.expand(player, children); 96 int n = children.size(); 97 for(int i = 0; i < n; ++i) { 98 int v = alphabeta(children[i], player ^ 1, alpha, beta); 99 if(!player) alpha = max(alpha, v); 100 else beta = min(beta, v); 101 if(beta <= alpha) break; 102 } 103 return !player ? alpha : beta; 104 } 105 106 int main() { 107 int t = 0; 108 while(scanf("%s", s) != EOF && *s != 'E') { 109 scanf("%d", &n); 110 for(int i = 0; i < 2 * n; ++i) { 111 scanf("%d%c", &deck[i], &c); 112 if(c == 'B') deck[i] = -deck[i]; 113 } 114 int start = !(deck[0] > 0); 115 State beg; 116 int ans = alphabeta(beg, start, -INF, INF); 117 printf("Case %d: ", ++t); 118 if(s[0] == 'B') ans = -ans; 119 if(ans == 0) puts("Axel and Birgit tie"); 120 else if(ans > 0) printf("%s wins %d ", s, ans); 121 else printf("%s loses %d ", s, -ans); 122 } 123 }