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);
}
目录
相关文章
|
9月前
Uva10001 Garden of Eden
Uva10001 Garden of Eden
32 0
|
7月前
uva 10340 all in all
输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变),得到字符串是。
21 0
|
机器学习/深度学习
uva 12470 Tribonacci
点击打开uva12470  思路: 矩阵快速幂 分析: 1 裸题 代码: /************************************************ * By: chenguolin ...
963 0
uva 1203 Argus
点击打开链接uva 1203 思路: 优先队列 分析: 1 题目要求前k个事件的编号,我们利用优先队列来维护即可 2 优先队列保存的是一个struct,因此我们需要重载 s.
1279 0
|
JavaScript 定位技术
uva 10047 - The Monocycle
点击打开链接uva 10047 思路:bfs 分析: 1 题目给定一个起始的状态然后要求是否可以到达目标状态 2 这些状态包括了位置,方向,底面颜色。
828 0
uva 11627 Slalom
点击打开链接uva 11627 思路:二分答案 分析: 1 给定S个滑雪板的速度,问是否可以找到一个滑板使得通过所有的门的时间最少,如果找不到输出IMPOSSIBLE 2 很明显的二分题目,我们知道了二分那应该怎么判断是否可以通过所有...
1046 0
uva10340 Ail in All
题意:输入两个字符串s和t,判断是否可以从t种删除0个或多个字符(其他字符不变),得到字符串s,比如abcde可以得到bce,单数无法得到dc 分析:简单模拟即可 1 #include 2 #include 3 #include 4 #define zz 5 usin...
555 0
UVA3295
题意:给出一个a*b的网格,在网格上取不共线的三点构成三角形,求三角形总数。分析:就是一一道简单的组合数计算题目,设总结点数为n,则取三个节点的个数为C(n,3),然后减去横向、竖向、斜向的三点共线的个数即可,斜线三点共线等价于所枚举的矩形的长宽成倍数关系,即gcd不为1 代码如下: #incl...
636 0