UVA 127 “Accordian” Patience

來源:互聯網
上載者:User

Description


 ``Accordian'' Patience 


You are to simulate the playing of games of ``Accordian'' patience, 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 neighbour on the left, or matches the third card to the left, it may 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 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 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 two cards may be moved, you should 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 a # 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
題目大概意思,有很多牌,從左至右不重疊地放在案頭上,如果當前牌和相鄰左邊第一張或第三張牌匹配(花色或者值相同)就把當前的牌壓在左邊的那堆牌上,依此類推,直到無法移動牌為止。
思路:類比唄,剛開始用vector做,速度慢啊2000多MS,後來改成list做,各種操蛋報錯,還是1A,1000M,還是比
蒲貴宇用C語言慢,他600MS。
LIST版:
language:c++
code:
#include<iostream>#include<stack>#include<list>#include<cstdio>using namespace std;struct card{    char x,y;    card(char a,char b):x(a),y(b) {}};bool judge(card a,card b){    if(a.x==b.x||a.y==b.y)return true;    return false;}list<stack<card> >::iterator pre3(const list<stack<card> >::iterator i){    list<stack<card> >::iterator a=i;    return ------a;}list<stack<card> >::iterator pre1(const list<stack<card> >::iterator i){    list<stack<card> >::iterator a=i;    return --a;}int main(){    char s[3];    list<stack<card> > cards;    list<stack<card> >::iterator i;    while(scanf("%s",s),s[0]!='#')    {        card c(s[0],s[1]);        stack<card>temp;        temp.push(c);        cards.push_back(temp);        if(cards.size()==52)        {            bool ismoved=1;            while(ismoved)            {                ismoved =false;                size_t count;                for(i=cards.begin(),count=0; i!=cards.end(); i++,count++)                {                    if(count>2&&judge(i->top(),pre3(i)->top()))                    {                        pre3(i)->push(i->top());                        i->pop();                        ismoved=true;                        if(i->empty())                            cards.erase(i);                        break;                    }                    if(count>0&&judge(i->top(),pre1(i)->top()))                    {                        pre1(i)->push(i->top());                        i->pop();                        ismoved=true;                        if(i->empty())                            cards.erase(i);                        break;                    }                }            }            if(cards.size()==1)                printf("%d pile remaining:",cards.size());            else                printf("%d piles remaining:",cards.size());            for(i=cards.begin(); i!=cards.end(); i++)            {                printf(" %d",i->size());            }            printf("\n");            ismoved=true;            cards.clear();        }    }    return 0;}

vector版:

language:c++
code:
#include<iostream>#include<stack>#include<vector>#include<cstdio>using namespace std;struct card{    char x,y;    card(char a,char b):x(a),y(b) {}};bool judge(card a,card b){    if(a.x==b.x||a.y==b.y)return true;    return false;}int main(){    char s[3];    bool ismoved=1;    vector<stack<card> > cards;    while(scanf("%s",s),s[0]!='#')    {        card c(s[0],s[1]);        stack<card>temp;        temp.push(c);        cards.push_back(temp);        if(cards.size()==52)        {            while(ismoved)            {                size_t i;                ismoved =false;                for(i=0; i<cards.size(); i++)                {                    if(i>2&&judge(cards[i].top(),cards[i-3].top()))                    {                        cards[i-3].push(cards[i].top());                        cards[i].pop();                        ismoved=true;                        if(cards[i].empty())                            cards.erase(cards.begin()+i);                        break;                    }                    if(i>0&&judge(cards[i].top(),cards[i-1].top()))                    {                        cards[i-1].push(cards[i].top());                        cards[i].pop();                        ismoved=true;                        if(cards[i].empty())                            cards.erase(cards.begin()+i);                        break;                    }                }            }            if(cards.size()==1)                printf("%d pile remaining:",cards.size());            else                printf("%d piles remaining:",cards.size());            for(size_t i=0; i!=cards.size(); i++)            {                printf(" %d",cards[i].size());            }            printf("\n");            ismoved=true;            cards.clear();        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.