uva127 "Accordian" Patience

#include <stdio.h>
#include <stdlib.h>

#define LOCAL
#define ERROR 0
#define OK 1
typedef  struct data
{
    char rank;
    char suit;
    struct data *next;
}Data;

typedef struct lnode
{
    int count;
    Data *top;
    struct lnode *next;
}LNode, *LinkList;

LinkList initList();
int GetElem(LNode *h, int i, LNode **e);
LNode *GetPrecursor(LinkList h, LNode *e);
int match(Data *a, Data *b);
void freespace(LNode *h);



int main()
{
    int ans[52];
    int i;
    int first;
    char str[3];
    LinkList h = NULL;
    int count = 0;
    int status;
    LNode *node , *e = NULL, *p = NULL, *pre = NULL;
    Data *q = NULL;
    int flag;
    Data *datanode = NULL;
    int piles;
   
   
#ifdef LOCAL
    freopen("c://uva_in.txt", "r", stdin);
    //freopen("c://uva_out.txt", "w", stdout);
#endif
   
    while (1)
    {
        scanf("%s", str);
        if (str[0] == '#')
            break;
        if (count == 0)
            h = initList();
       
       
        datanode = (Data *)malloc(sizeof(Data));
        datanode->rank = str[0];
        datanode->suit = str[1];
        datanode->next = NULL;
       
       
        node = (LNode *)malloc(sizeof(LNode));
        node->top = (Data *)malloc(sizeof(Data));
        node->top->next = datanode;
        node->count = 1;
       
       
        node->next = h->next;
        h->next = node;
       
        p = h->next;
        while (1)
        {
            flag = 1;
            while (flag)
            {
                flag = 0;
                status = GetElem(p, 3, &e);
                if (status == ERROR || (status == OK && !match(p->top->next, e->top->next)))   
                    status = GetElem(p, 1, &e);
               
                if (status == OK && match(p->top->next, e->top->next))
                {
                    flag = 1;
                   
                   
                    q = p->top->next;
                    p->top->next = q->next;
                    q->next = e->top->next;
                    e->top->next = q;
                    e->count++;
                    p->count--;
                   
                    if (p->count == 0)
                    {
                        pre = GetPrecursor(h, p);
                        pre->next = p->next;
                        free(p);
                    }
                    p = e;
                   
                   
                }
            }
           
            pre = GetPrecursor(h, p);
            if (pre != h)
                p = pre;
            else break;
        }
       
        count++;
       
        if (count == 52)
        {
            p = h->next;
            i = 0;

            while (p)
            {
                ans[i++] = p->count;
                p = p->next;
            }
            piles = i;

           
            if (piles == 1)
                printf("%d pile remaining: ", piles);
            else
                printf("%d piles remaining: ", piles);
           
           
            for (i = piles - 1, first = 1; i>= 0; i--)
            {
                if (first)
                    first = 0;
                else
                    printf(" ");
               
                printf("%d", ans[i]);
            }
           
            printf("/n");
            freespace(h);
            count = 0;
        }
       
    }
   
   
    return 0;
}

LinkList initList()
{
    LNode *p = (LNode *)malloc(sizeof(LNode));
    p->top = NULL;
    p->next = NULL;
    return p;
}

int GetElem(LNode *h, int i, LNode **e)
{
    LNode *p = h;
    int j = 0;
   
    while (p && j < i)
    {
        p = p->next;
        j++;
    }
   
    if (!p)
        return ERROR;
   
    *e = p;
    return OK;
}

 

//获得该结点的前驱

LNode *GetPrecursor(LNode *h, LNode *e)
{
    LNode *p = h;
    while (p->next != e)
        p = p->next;
   
    return p;
}

int match(Data *a, Data *b)
{
    if (a->rank == b->rank || a->suit == b->suit)
        return 1;
    else
        return 0;
}

void freespace(LNode *h)
{
    LNode *p;
    Data *q;
    if (h->next)
    {
        p = h->next;
        while (p->top->next)
        {
            q = p->top->next;
            p ->top->next = q->next;
            free(q);
        }
        free(p->top);
        freespace(h->next);
    }
    free(h);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值