MySQL data types and usage

Source: Internet
Author: User

' Accordian ' patience

You is to simulate the playing of the Games of "Accordian" patience, the rules for which is as follows:

Deal cards one by one in a row from left to right, not overlappin G. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left,  it  may be moved onto this card. Cards match if they is of the same suit or same rank. After making a move, the look-to-see if it has made additional moves possible. Only the top card of each pile may moved at any given time. Gaps between piles should 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 of the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile.

Situations can arise where more than one play is possible. Where The cards May is moved, you should adopt the strategy of always moving the leftmost card possible. Where a card is 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 is dealt from the pack. The input contains pairs of lines, each line containing a cards separated by a single space characters. The final line of the input file contains a # as its first character. Cards is represented as a and 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 are the suit (c= Clubs, D=diamonds, H=hearts, S=spades).

Output

One line of output must is produced for each pair of lines (that between them describe a pack of cards) in the input. Each line of output shows the number of cards in all 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 6 C 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 pile remaining:52

Solution:

First Test instructions to understand carefully, and then simulation can, however, because the processing of a large number of characters, must be entered with scanf, and scanf can not input string type, so you can not use string, to use a character array, if you use string, you must assigned the word to string, It's time-consuming, because of this, the tle here several times. Then the simulation, with the vector, to more than 2000 milliseconds, with the list may be a little more.

Code:

#include <iostream> #include <cstdio> #include <string> #include <vector> #include <stack >using namespace Std;struct node{char x, y;};    BOOL CMP (node A,node b) {if (a.x==b.x) return true;    if (A.Y==B.Y) return true; return false;}        int main () {while (1) {Vector<stack<node > >card;//defines a vector (container) that contains an element type of stack<node>;        Char temp[3];        scanf ("%c", &temp[0]);        if (temp[0]== ' # ') break;        scanf ("%c", &temp[1]);        Stack<node > P;        Node C;        C.X=TEMP[0];C.Y=TEMP[1];        P.push (c);        Card.push_back (P);//Put the stack in the card container.            for (int i=1;i<52;i++) {getchar ();            scanf ("%c%c", &temp[0],&temp[1]);            Node C;            C.X=TEMP[0];C.Y=TEMP[1];            Stack<node > t;            T.push (c);        Card.push_back (t);       }/* int len=card.size (); for (int i=0;i<len;i++) {cout<<card[i].top () << Endl;            } getchar (); */int len=card.size ();//Find container size for (int i=0;i<len;i++) {int j=i;                        if (j-3>=0) {if (CMP (Card[j-3].top (), Card[j].top ())) {//container can also be used as an array by subscript.                        Card[j-3].push (Card[j].top ());                        Card[j].pop (); if (Card[j].empty ()) {Card.erase (Card.begin () +j),//delete the stack at a location, the parameter must be a pointer to that position                        Needle Len=card.size ();                   } i=-1;                    Continue                        }} if (j-1>=0) {if (CMP (Card[j-1].top (), Card[j].top ())) {                        Card[j-1].push (Card[j].top ());                        Card[j].pop ();                            if (Card[j].empty ()) {Card.erase (Card.begin () +j);            Len=card.size ();            } i=-1;                    Continue        }}} int l=card.size ();            if (l>1) {printf ("%d piles remaining:", L);        for (int i=0;i<l;i++) printf ("%d", card[i].size ());                } else {printf ("%d pile remaining:", L);        printf ("%d", card[0].size ());        } printf ("\ n");    GetChar (); }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.