您的位置:首页 > 其它

uva127 "Accordian" Patience

2010-10-10 13:18 309 查看
#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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  null struct include c