"Accordian" PatienceUVA127 ("Accordion" card game)

Source: Internet
Author: User
52 poker cards are arranged from left to right on the plane according to the following rules: 1. in the order from left to right, if a card matches the first or third card on the left, it is placed on the corresponding card. 2. if you can move to multiple positions, move to the leftmost card. (Match: the color or value are the same) Note: only move at a time



"Accordian" Patience

From: Ultraviolet A, 127

Submit

Time Limit: 3000 MS

You are to simulate the playing of games of ''accored'', the rules for which are as follows:

Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbor on the left, or matches the third card to the left, ItMay be moved onto that card. cards match if they are of the same suit or same rank. after making a move, look to see if it has made additional moves possible. only the top card of each pile may be moved at any given time. gaps between piles shoshould be closed up as soon as they appear by moving all piles on the right of the gap one position to the left. deal out the whole pack, combining cards towards the left whenever possible. the game is won if the pack is already CED to a single pile.

Situations can arise where more than one play is possible. where two cards may be moved, you shoshould adopt the strategy of always moving the leftmost card possible. where a card may be moved either one position to the left or three positions to the left, move it three positions.

Input

Input data to the program specifies the order in which cards are dealt from the pack. the input contains pairs of lines, each line containing 26 cards separated by single space characters. the final line of the input file contains#As its first character. cards are represented as a two character code. the first character is the face-value (A = Ace, 2-9, T = 10, J = Jack, Q = Queen, K = King) and the second character is the suit (C = Clubs, D = Diamonds, H = Hearts, S = Spades ).

Output

One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input. each line of output shows the number of cards in each of the piles remaining after playing ''accordian patience ''with the pack of cards as described by the corresponding pairs of input lines.

Sample Input

QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5CAC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KDAH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS#

Sample Output

6 piles remaining: 40 8 1 1 1 11 pile remaining: 52

Question:

52 poker cards are arranged from left to right on the plane according to the following rules:

1. in the order from left to right, if a card matches the first or third card on the left, it is placed on the corresponding card.

2. if you can move to multiple positions, move to the leftmost card. (Matching: the color or value are the same)

Note: Only the top cards of each stack are moved at a time.

Solutions: vector/list, stack, and simulation. Create a stack for each stacked card and simulate it.

Code 1: (list + stack)

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; struct card {char x, y; card (char x0, char y0) {x = x0, y = y0 ;}; list <stack
     
      
> Cards; list
      
        >:: Iterator it; bool judge (card a, card B) {// you can change the condition if (. x = B. x |. y = B. y) return true; return false;} list <stack
       
         >:: Iterator pre1 (const list <stack
        
          >:: Iterator I) {list <stack
         
           >:: Iterator it = I; // function: return the first address on the left, return -- it;} list
          
            >:: Iterator pre3 (const list <stack
           
             >:: Iterator I) {list
            
              >:: Iterator it = I; // Function: return ------ it when the third image on the left is displayed; // return the first three addresses} bool input () {char s [3]; while (~ Scanf ("% s", s) {if (s [0] = '#') return false; card c (s [0], s [1]); stack
             
               Temp; // stack is push temp. push (c); // The list is push_back cards. push_back (temp); if (cards. size () = 52) return true;} void solve () {bool ismoved = true; while (ismoved) {ismoved = false; size_t cnt; // unsigned int; for (it = cards. begin (), cnt = 0; it! = Cards. end (); it ++, cnt ++) {if (cnt> 2 & judge (it-> top (), pre3 (it)-> top ())) {pre3 (it)-> push (it-> top (); it-> pop (); ismoved = true; if (it-> empty () {cards. erase (it);} break;} if (cnt> 0 & judge (it-> top (), pre1 (it)-> top ())) {pre1 (it)-> push (it-> top (); it-> pop (); ismoved = true; if (it-> empty () {cards. erase (it) ;}break ;}}} void outResult () {if (cards. size () = 1) {printf ("% d pile remaining:", cards. size ();} els E printf ("% d piles remaining:", cards. size (); for (it = cards. begin (); it! = Cards. end (); it ++) {printf ("% d", it-> size (); // can only be accessed with an iterator, cannot use subscript} printf ("\ n"); cards. clear () ;}int main () {while (input () {solve (); outResult () ;}return 0 ;}
             
            
           
          
         
        
       
      
     
    
   
  
 

Code 2: (vector + stack)

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; struct card {char x, y; card (char x0, char y0) {x = x0, y = y0 ;}; vector <stack
     
      
> Cards; bool judge (card a, card B) {// Determine the situation where the switch can be performed if (. x = B. x |. y = B. y) return true; return false;} int pre1 (int I) {int it = I; // function: when the first one on the left is equal, return -- it;} int pre3 (int I) {int it = I; // function: return ------ it; // return the first three addresses} bool input () {char s [3]; while (~ Scanf ("% s", s) {if (s [0] = '#') return false; card c (s [0], s [1]); stack
      
        Temp; // stack is push temp. push (c); // The list is push_back cards. push_back (temp); if (cards. size () = 52) return true;} void solve () {bool ismoved = true; while (ismoved) {ismoved = false; for (int it = 0; it! = Cards. size (); it ++) {if (it> 2 & judge (cards [it]. top (), cards [it-3]. top () {cards [it-3]. push (cards [it]. top (); cards [it]. pop (); ismoved = true; if (cards [it]. empty () {cards. erase (cards. begin () + it);} break;} if (it> 0 & judge (cards [it]. top (), cards [it-1]. top () {cards [it-1]. push (cards [it]. top (); cards [it]. pop (); ismoved = true; if (cards [it]. empty () {cards. erase (cards. begin () + it) ;}break ;}}} void outResult () {if (cards. size () = 1) {printf ("% d pile remaining:", cards. size ();} else printf ("% d piles remaining:", cards. size (); for (int it = 0; it
       
        


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.