[UVA 246] [deque]10-20-30

Source: Internet
Author: User

10-20-30

A simple solitaire card game called 10-20-30 uses a standard deck of the playing cards in which suit is irrelevant. The value of a face card (King, Queen, Jack) is 10. The value of an aces is one. The value of each of the cards are the face value of the the card (2, 3, 4, etc.). Cards is dealt from the top of the deck. You begin by dealing out seven cards and left to right forming seven piles. After playing a card on the rightmost pile, the next pile upon which your play a card is the leftmost pile.

For each card placed on a pile, check this pile to see if one of the following three card combinations totals, or 3 0.

  1.  the first and last one,

2. The first one and the last one, 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 could expose three more cards that can is picked up. If So, the pick them up. Continue until no more sets of three can is picked up from the pile.

For example, suppose a pile contains 5 9 7 3 where the 5 are at the first card of the pile, and then a 6 is played. The first and 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 are now the 6, the card above it's the 9, and the one above the 9 is the 5.

If a queen were played instead of the six, 5 + 9 + ten =, and 5 + 3 + ten =, but 7 + 3 + ten =, so the last three CA RDS would is picked up, leaving the pile as 5 9.

If A pile contains only three cards when the three sum to ten, OR and then the pile "disappears" when the cards is pic ked up. That's, subsequent play skips over the position, the now-empty pile occupied. You win if all the piles disappear. You lose if is unable to deal a card. It is also possible to has a draw if neither of the previous of the conditions ever occurs.

Write a program, that'll play games of 10-20-30 given initial card decks as input.

Input

Each input set consists of sequence of the 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 I S 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 8 2 3 9 The 8 4 5 1 7 6 7 2 6 9 2 3 3 4 4 9 1 5 10 10 1 8 10 7 8 10 6 10 10 10 9   

Sample Output

win:66loss:82draw:73


Write this question is to learn the use of deque, mobile card heap or similar to the wording of the problem, with the number of POS set up a location to the heap map, to note that after the removal of a heap subscript to reduce, to ensure the next access to the current location of the heap, this I trick a long time.
Learning the difference between the resize and the reserve, these two are worth noting in terms of efficiency. Remember to assign value after resize.
If you write your own class, you want to use set to judge weight, to overload the operator < available memcpy implementations. 
#include <cstdio>#include<queue>#include<Set>#include<vector>using namespacestd;Const intMAXN = -;Char*ans[3] = {"Draw:","Loss:","Win:"};vector<deque<int> >s (8);d Eque<int> &hand = s[0];intpos[7];intPlsz;intStep;inlineBOOLPick (deque<int> &x) {   intn =x.size (); intsum; if(sum = x[0] + x[1] + x[n-1], sum = =Ten|| sum = = -|| sum = = -){//Hand.push_back (x[0]); Hand.push_back (x[1]); Hand.push_back (x[n-1]); X.pop_front ();  X.pop_front ();      X.pop_back (); return true; }   if(n>3){      if(sum = x[0]+x[n-2]+x[n-1], sum = =Ten|| sum = = -|| sum = = -) {hand.push_back (x[0]); Hand.push_back (x[n-2]); Hand.push_back (x[n-1]);  X.pop_front (); X.pop_back ();         X.pop_back (); return true; }      if(sum = x[n-3]+x[n-2]+x[n-1], sum = =Ten|| sum = = -|| sum = = -) {hand.push_back (x[n-3]); Hand.push_back (x[n-2]); Hand.push_back (x[n-1]);  X.pop_back ();  X.pop_back ();         X.pop_back (); return true; }   }   return false;}intSovle () {Set<vector<deque<int> > >Vis;   Vis.insert (s); intT,id;  for(;;) {       for(inti =0; i < PLSZ; i++) {         if(Hand.empty ())return 1; T=Hand.front (); Hand.pop_front (); Step++; ID=Pos[i];         S[id].push_back (t);  while(S[id].size () >=3&&pick (S[id])) {            if(S[id].empty ()) {Plsz--; if(Plsz = =0)return 2;  for(intj = I;j < plsz;j++) {//Moving a heapPOS[J] = pos[j+1]; } I--;//access the heap after the move            }         }         if(Vis.count (s) = =0) Vis.insert (s); Else return 0; }   }   return 0;}BOOLRead () {intT; scanf ("%d",&t); if(T = =0)return false; Step=0;  for(inti =1; I <8; i++) s[i].clear (); Hand.resize ( the);  for(inti =0; I <7; i++) Pos[i]= i+1; Plsz=7; Deque<int>:: Iterator it; hand[0] =T;  for(it = Hand.begin (), it++; it! = Hand.end (); it++) scanf ("%d", &t), *it =T; return true;}intMain () {//freopen ("In.txt", "R", stdin);    while(Read ()) {intFlag =Sovle (); printf ("%s%d\n", Ans[flag],step); }   return 0;}



[UVA 246] [deque]10-20-30

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.