UV-246 10-20-30 (simulation + STL)

Source: Internet
Author: User

Description


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 wocould 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 110 1 10 10 10 3 10 9 8 10 8 7 1 2 8 6 7 3 3 8 24 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 72 6 10 10 4 10 1 3 10 1 1 10 2 2 10 4 10 7 7 1010 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 110   5 10 10 1 8 10 7 8 10 6 10 10 10 9 6 2 10 100
Sample output
Win: 66 loss: 82 draw: 73
Question: a card game is called 10-20-30. There are 52 cards in total, regardless of the color. The values of K, Q, and J are considered as 10, the value of other cards is regarded as the card number, starting from the top of the card stack. Start with seven cards and Form seven groups from left to right. Issue a card to the rightmost group and then issue a card to the leftmost group. If a problem occurs in the card Stack: the total values of the first two and last cards, the total values of the first and last two cards, or the total values of the last three cards are 10, 20, or 30, place the three cards at the bottom of the card stack. If a group of cards has only three cards and their total value is 10, 20, or 30, the cards will be removed and the licensing will skip this position, if all the cards are lost, the heap wins. If no cards are sent, the heap loses. Otherwise, the heap loses. Input: Enter the nominal value of 52 cards. Output: winning and losing results and the number of cards issued; Train of Thought: Simulate + STL, and use map to determine the weight
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <deque>#include <map>using namespace std;struct state {int v[70];state() {memset(v, 0, sizeof(v));}bool operator <(const state &x) const {return memcmp(v, x.v, sizeof(state)) < 0;}};deque<int> pile[10];queue<int> hand;map<state, int> record;void reduce(deque<int> &pile) {while (pile.size() >= 3) {int n = pile.size();if ((pile[0]+pile[1]+pile[n-1]) % 10 == 0) {hand.push(pile[0]);hand.push(pile[1]);hand.push(pile[n-1]);pile.pop_front();pile.pop_front();pile.pop_back();} else if ((pile[0]+pile[n-2]+pile[n-1]) % 10 == 0) {hand.push(pile[0]);hand.push(pile[n-2]);hand.push(pile[n-1]);pile.pop_front();pile.pop_back();pile.pop_back();} else if ((pile[n-3]+pile[n-2]+pile[n-1]) % 10 == 0) {hand.push(pile[n-3]);hand.push(pile[n-2]);hand.push(pile[n-1]);pile.pop_back();pile.pop_back();pile.pop_back();}else {return;}}}int main() {int x;while (1) {while (!hand.empty()) hand.pop();for (int i = 0; i < 7; i++)pile[i].clear();for (int i = 0; i < 52; i++) {scanf("%d", &x);if (x == 0) return 0;hand.push(x);}for (int i = 0; i < 7; i++)pile[i].push_back(hand.front()), hand.pop();for (int i = 0; i < 7; i++)pile[i].push_back(hand.front()), hand.pop();int flag = 0, step = 14;while (!flag) {for (int i = 0; i < 7; i++) {if (pile[i].size() == 0)continue;step++;pile[i].push_back(hand.front());hand.pop();reduce(pile[i]);if (hand.size() == 52) {printf("Win : %d\n", step);flag = 1;break;}if (hand.size() == 0) {printf("Loss: %d\n", step++);flag = 1;break;}state s;int m = 0;for (int j = 0; j < 7; j++) {for (int k = 0; k < pile[j].size(); k++)s.v[m++] = pile[j][k];s.v[m++] = 15;}queue<int> q = hand;while (!q.empty()) {s.v[m++] = q.front();q.pop();}s.v[m++] = 15;if (record.find(s) != record.end()) {printf("Draw: %d\n", step);flag = 1;break;}record[s]++;}}}return 0;}



UV-246 10-20-30 (simulation + STL)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.