Uva - 246 - 10-20-30
A simple solitaire card game called 10-20-30 uses a standard deck of 52 playing cards in which suit is irrelevant. The value of a face card (king, queen, jack) is 10. The value of an ace is one. The value of each of the other cards is the face value of the card (2, 3, 4, etc.). Cards are dealt from the top of the deck. You begin by dealing out seven cards, left to right forming seven piles. After playing a card on the rightmost pile, the next pile upon which you play a card is the leftmost pile.
For each card placed on a pile, check that pile to see if one of the following three card combinations totals 10, 20, or 30.
1. the first two and last one,2. the first one and the last two, or
3. the last three cards.
If so, pick up the three cards and place them on the bottom of the deck. For this problem, always check the pile in the order just described. Collect the cards in the order they appear on the pile and put them at the bottom of the deck. Picking up three cards may expose three more cards that can be picked up. If so, pick them up. Continue until no more sets of three can be picked up from the pile.
For example, suppose a pile contains 5 9 7 3 where the 5 is at the first card of the pile, and then a 6 is played. The first two cards plus the last card (5 + 9 + 6) sum to 20. The new contents of the pile after picking up those three cards becomes 7 3. Also, the bottommost card in the deck is now the 6, the card above it is the 9, and the one above the 9 is the 5.
If a queen were played instead of the six, 5 + 9 + 10 = 24, and 5 + 3 + 10 = 18, but 7 + 3 + 10 = 20, so the last three cards would be picked up, leaving the pile as 5 9.
If a pile contains only three cards when the three sum to 10, 20, or 30, then the pile "disappears" when the cards are picked up. That is, subsequent play skips over the position that the now-empty pile occupied. You win if all the piles disappear. You lose if you are unable to deal a card. It is also possible to have a draw if neither of the previous two conditions ever occurs.
Write a program that will play games of 10-20-30 given initial card decks as input.
Input
Each input set consists of a sequence of 52 integers separated by spaces and/or ends of line. The integers represent card values of the initial deck for that game. The first integer is the top card of the deck. Input is terminated by a single zero (0) following the last deck.
Output
For each input set, print whether the result of the game is a win, loss, or a draw, and print the number of times a card is dealt before the game results can be determined. (A draw occurs as soon as the state of the game is repeated.) Use the format shown in the ``Sample Output" section.
Sample Input
2 6 5 10 10 4 10 10 10 4 5 10 4 5 10 9 7 6 1 7 6 9 5 3 10 10 4 10 9 2 1 10 1 10 10 10 3 10 9 8 10 8 7 1 2 8 6 7 3 3 8 2 4 3 2 10 8 10 6 8 9 5 8 10 5 3 5 4 6 9 9 1 7 6 3 5 10 10 8 10 9 10 10 7 2 6 10 10 4 10 1 3 10 1 1 10 2 2 10 4 10 7 7 10 10 5 4 3 5 7 10 8 2 3 9 10 8 4 5 1 7 6 7 2 6 9 10 2 3 10 3 4 4 9 10 1 1 10 5 10 10 1 8 10 7 8 10 6 10 10 10 9 6 2 10 10 0
Sample Output
Win : 66 Loss: 82 Draw: 73
模拟题,用双端队列做就行了。。。早晨因为一个小逻辑错误,调试了好久,尴尬。。。。
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> using namespace std; //双端队列比较好处理 vector<deque<int> > piles; // 用一个set存放状态,每次检查 set<vector<deque<int> > > vis; int n, zero[8]; // zero记录每个牌堆是否为空 int first, second, third, sum; void init() { piles.clear(); vis.clear(); memset(zero, 0, sizeof(zero)); // 初始化数组 for (int i = 0; i < 8; i++) { piles.push_back(deque<int>()); } // 前7张牌放入7个牌堆中 piles[1].push_back(n); for (int i = 2; i <= 7; i++) { scanf("%d", &n); piles[i].push_back(n); } // 读取剩下的45张牌,放到手牌中 for (int i = 0; i < 45; i++) { scanf("%d", &n); piles[0].push_back(n); } } // 最上面两张和最下面一张 bool checkSum1(int i) { first = piles[i].front(); piles[i].pop_front(); second = piles[i].front(); third = piles[i].back(); sum = first + second + third; if (10 == sum || 20 == sum || 30 == sum) { piles[0].push_back(first); piles[0].push_back(second); piles[0].push_back(third); piles[i].pop_front(); piles[i].pop_back(); return true; } piles[i].push_front(first); return false; } // 最上面一张和最下面两张 bool checkSum2(int i) { first = piles[i].front(); third = piles[i].back(); piles[i].pop_back(); second = piles[i].back(); sum = first + second + third; if (10 == sum || 20 == sum || 30 == sum) { piles[0].push_back(first); piles[0].push_back(second); piles[0].push_back(third); piles[i].pop_front(); piles[i].pop_back(); return true; } piles[i].push_back(third); return false; } // 最下面三张 bool checkSum3(int i) { third = piles[i].back(); piles[i].pop_back(); second = piles[i].back(); piles[i].pop_back(); first = piles[i].back(); sum = first + second + third; if (10 == sum || 20 == sum || 30 == sum) { piles[0].push_back(first); piles[0].push_back(second); piles[0].push_back(third); piles[i].pop_back(); return true; } piles[i].push_back(second); piles[i].push_back(third); return false; } bool checkSum(int i) { if (piles[i].size() < 3) { return false; } if (checkSum1(i)) { return true; } if (checkSum2(i)) { return true; } if (checkSum3(i)) { return true; } return false; } void solve() { int i = 1; for (int cnt = 8;; cnt++) { int newCard = piles[0].front(); piles[0].pop_front(); piles[i].push_back(newCard); while (checkSum(i)); if (piles[i].size() == 0) { zero[i] = 1; } if (vis.find(piles) != vis.end()) { // 如果找到了 printf("Draw: %d\n", cnt); return; } vis.insert(piles); if (piles[0].size() == 0) { printf("Loss: %d\n", cnt); return; } if (piles[0].size() == 52) { printf("Win : %d\n", cnt); return; } i = i % 7 + 1; while (zero[i]) { i = i % 7 + 1; } } } int main() { while (scanf("%d", &n) && n) { init(); solve(); } return 0; }