UVa 127 - "Accordian" Patience

``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 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
#

Sample Output

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

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int a[1000005],q[1000005];
char s[5];
struct pile
{
    int top;
    char card[55][5];
    struct pile *next;
    struct pile *pre;
};
typedef struct pile node;
node *head ,*tail,*pnew;

node * creat()
{
    head=(node*)malloc (sizeof(node));
    head->next=NULL;
    tail=head;
    pnew=(node*)malloc (sizeof(node));
    strcpy(pnew->card[0],s);
    pnew->next=NULL;
    pnew->top=0;
    pnew->pre=NULL;
    tail->next=pnew;
    tail=pnew;
    for(int i=1;i<52;i++)
    {
        scanf("%s",s);
        pnew=(node*)malloc (sizeof(node));
        strcpy(pnew->card[0],s);
        pnew->next=NULL;
        pnew->top=0;
        pnew->pre=tail;
        tail->next=pnew;
        tail=pnew;
    }
    return head;
}

void deletep (node *p)
{
    node *pre ,*next;
    pre=p->pre;
    next=p->next;
    pre->next=next;
    if(next)next->pre=pre;
    free(p);
}

int main()
{
    while(scanf("%s",s)!=EOF)
    {
        node *head,*p,*q,*o;    int d=0;
        if(s[0]=='#')
            break;
        p=creat();head=p;
        p=p->next;
        while(1)
        {
            int flag=0;
            p=p->next;
            if(p==NULL)break;
            int ptop,qtop,otop;
            ptop=p->top;
            q=o=p->pre;qtop=q->top;
            if(o!=NULL)o=o->pre;
            if(o!=NULL)o=o->pre;
            if(o!=NULL)otop=o->top;
            if(o!=NULL&&(p->card[ptop][0]==o->card[otop][0]||p->card[ptop][1]==o->card[otop][1]))
            {
                        o->top++;
                        otop=o->top;
                        strcpy(o->card[otop],p->card[ptop]);
                        p->top--;
                        flag=1;
            }
                else if(q!=NULL&&(p->card[ptop][0]==q->card[qtop][0]||p->card[ptop][1]==q->card[qtop][1]))
                {
                    q->top++;
                    qtop=q->top;
                    strcpy(q->card[qtop],p->card[ptop]);
                    p->top--;
                    flag=1;
                }
            if(p->top<0) deletep(p);
            if(flag)
            {
                p=head;
                p=p->next;
            }
        }
        int c=0;
        node *a=head;
    while(a)
    {
        a=a->next;if(a)
        c++;
    }if(c==1) printf("%d pile remaining:",c);
    else
    printf("%d piles remaining:",c);
    while(head)
    {
        head=head->next;if(head)
        printf(" %d",head->top+1);
    }
    printf("\n");
    }
    return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值