UVa 127: "Accordian" patience data structure topics

Source: Internet
Author: User
Tags continue prev printf

Topic Link:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=63

Type of topic: data structure, linked list

Meaning

Send a deck of cards (52 pieces, two lines), starting from left to right, for the current card, if the value (face-value) or shape (suit) of the first or the left is the same, move the card to the first or third row on the left (if the left 1 and the left 3 can be moved, Move to the left 3, and then continue moving until it is moved, if the move condition continues to be satisfied. When a stack of cards is empty, "empty" is eliminated.

Ideas for solving problems:

This problem is simple simulation, for each stack of cards, with a stack to save. The key to disintegration is the process of eliminating "empty stacks". Start with an array, but because the array to delete an element to move a lot, inefficient, resulting in tle. And then ready to use the linked list to solve. I was going to use the STL list at first, but I found it very uncomfortable. So I wrote the list of the implementation of the array to do, successful AC.

10269644 127 "Accordian" patience Accepted C++ 0.412 2012-06-29 04:26:42

This paper url:http://www.bianceng.cn/programming/sjjg/201410/45628.htm

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 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 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
#
Output:

6 Piles remaining:40 8 1 1 1 1

1 Pile remaining:52

#include <iostream> #include <cstdio> #include <stack> using namespace std;  
    
int nsize;  
struct string{char face[3];  
    
};  
struct pile{stack<string>face;  
}ARR[100];  
    
int prev[100], next[100];  
    Eliminates "empty stack" void Delgaps () {int i,j;  
            for (i=0 i!=nsize; I=next[i]) {if (Arr[i].face.empty ()) {Next[prev[i]] = next[i];  
            Prev[next[i]] = prev[i];  
        return;  
    ()}///Move the token bool moves () {int cnt=1;  
        for (int i=next[0]; i!=nsize i=next[i],++cnt) {int last3, last1; if (cnt>2) {//If the current position is above 3, the first decision can be moved to the left 3 Last3 = Prev[prev[prev[i]]];//Get the coordinates of left 3 if (arr[i). Face.top (). Face[0]==arr[last3].face.top (). face[0] | |  
                Arr[i].face.top (). Face[1]==arr[last3].face.top (). Face[1]) {Arr[last3].face.push (Arr[i].face.top ());  
  Arr[i].face.pop ();              return true;  }} last1= Prev[i]; Gets the coordinates of the left 1 if (Arr[i].face.top (). Face[0]==arr[last1].face.top (). face[0] | | arr[i].face.top (). f  
            Ace[1]==arr[last1].face.top (). Face[1]) {Arr[last1].face.push (Arr[i].face.top ());  
            Arr[i].face.pop ();  
        return true;  
}//Do not continue to move, return false returned false;  } void Solve () {//As long as you can move, keep moving while (move ()) delgaps ();  
    After each move, to the process of eliminating the empty heap} int main () {freopen ("Input.txt", "R", stdin);  
    nsize=0;  
    String temp;  
        while (scanf ("%s", Temp.face)) {if (temp.face[0]== ' @ ') break;  
        while (!arr[nsize].face.empty ()) Arr[nsize].face.pop ();  
            
        
        Arr[nsize].face.push (temp);  
        Prev[nsize] = nSize-1;  
        Next[nsize] = nsize+1;  
            
        ++nsize; if (nsize==52) {soLve ();  
            int num=0;  
               
            for (int i=0; i!=nsize; i=next[i]) ++num;   if (num==1) printf ("1 pile remaining:");  
                
            POJ This problem, this pile also has plus s else printf ("%d piles remaining:", num);  
                for (int i=0; i!=nsize; I=next[i]) {if (i) printf ("");  
            printf ("%d", arr[i].face.size ());  
            printf ("\ n");  
        nsize=0;  
} return 0; }

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.