uva127 "Accordian" Patience

简介: uva127 "Accordian" Patience
#include <stdio.h>#include <stdlib.h>#define LOCAL#define ERROR 0#define OK 1typedefstructdata{
charrank;
charsuit;
structdata*next;
}Data;
typedefstructlnode{
intcount;
Data*top;
structlnode*next;
}LNode, *LinkList;
LinkListinitList();
intGetElem(LNode*h, inti, LNode**e);
LNode*GetPrecursor(LinkListh, LNode*e);
intmatch(Data*a, Data*b);
voidfreespace(LNode*h);
intmain()
{
intans[52];
inti;
intfirst;
charstr[3];
LinkListh=NULL;
intcount=0;
intstatus;
LNode*node , *e=NULL, *p=NULL, *pre=NULL;
Data*q=NULL;
intflag;
Data*datanode=NULL;
intpiles;
#ifdef LOCALfreopen("c://uva_in.txt", "r", stdin);
//freopen("c://uva_out.txt", "w", stdout);#endifwhile (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;
elsebreak;
        }
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);
elseprintf("%d piles remaining: ", piles);
for (i=piles-1, first=1; i>=0; i--)
            {
if (first)
first=0;
elseprintf(" ");
printf("%d", ans[i]);
            }
printf("/n");
freespace(h);
count=0;
        }
    }
return0;
}
LinkListinitList()
{
LNode*p= (LNode*)malloc(sizeof(LNode));
p->top=NULL;
p->next=NULL;
returnp;
}
intGetElem(LNode*h, inti, LNode**e)
{
LNode*p=h;
intj=0;
while (p&&j<i)
    {
p=p->next;
j++;
    }
if (!p)
returnERROR;
*e=p;
returnOK;
}
//获得该结点的前驱LNode*GetPrecursor(LNode*h, LNode*e)
{
LNode*p=h;
while (p->next!=e)
p=p->next;
returnp;
}
intmatch(Data*a, Data*b)
{
if (a->rank==b->rank||a->suit==b->suit)
return1;
elsereturn0;
}
voidfreespace(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月前
Uva10001 Garden of Eden
Uva10001 Garden of Eden
33 0
|
8月前
uva 10340 all in all
输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变),得到字符串是。
23 0
|
10月前
UVa10123 No Tipping
UVa10123 No Tipping
41 0
|
10月前
UVa11968 - In The Airport
UVa11968 - In The Airport
38 0
UVa 10082 WERTYU
Problem Description A common typing error is to place the hands on the keyboard one row to the right of the correct position.
864 0
uva 10273 Eat or Not to Eat?
点击打开链接uva 10273 思路: 暴力求解 分析: 1 题目要求没有吃掉的奶牛的个数已经最后一次吃掉奶牛的天数 2 没有其它的方法只能暴力,对于n头牛的n个周期求最小公倍数,然后在2个公倍数之内暴力求解 代码: #inclu...
791 0
|
人工智能
uva 305 Joseph
点击打开链接uva 305 思路: 数学+打表 分析: 1 传统的约瑟夫问题是给定n个人和m,每次数m次把当前这个人踢出局,问最后留下的一个人的编号 2 这一题是前k个人是好人,后面k个是坏人。
1021 0
uva 10054 - The Necklace
点击打开链接uva 10054 思路: 欧拉回路 分析: 1 对于一个无向图来说如果这个图是一个欧拉图,那么必须满足该图是连通的并且每个点的度数都是偶数 2 题目给定n条边的无向图问我们是否是一个欧拉图,是的话输出欧拉图的一条路径 3 ...
823 0