纠结了2天,还是没发现错误用例在那里,提交即WA。
代码思路:
1.把当前所发的牌,在最后一排增加一堆。若这张牌能能被叠在左边第一堆或者第三堆,则不将添加的这堆写入记录中,否则写入
2.每次发牌后更新操作,从左往右更新。若有牌移动,则需要继续更新。
3.发完52张牌并做更新后,即输出结果。#include <stdio.h>
#include <stdlib.h>#include <string.h>
#include <math.h>
#include <ctype.h>
#define MAX(_a,_b) ((_a>_b)? _a:_b)
#define MIN(_a,_b) ((_a>_b)? _b:_a)
#define DEBUG
typedef struct stack{
int num;
int pos[52];
}mystack;
char input[52][5];
int curPile;
mystack* pcur;
mystack* ppre;
void stackcpy(mystack* dst,const mystack* src)
{
int i;
int len = src->num;
dst->num = len;
for(i=0;i<len;i++)
dst->pos[i] = src->pos[i];
}
int update()
{
int i,j;
int headi,headj;
int numtemp,k;
for(i=0;i<curPile-1;i++)
{
headi = pcur[i].num - 1;
for(j=i+1;j<curPile;j++)
{
if((j==i+1) || (j==i+3))
{
headj = pcur[j].num - 1;
if((input[pcur[i].pos[headi]][0] == input[pcur[j].pos[headj]][0]) || (input[pcur[i].pos[headi]][1] == input[pcur[j].pos[headj]][1]))
{
pcur[i].pos[++headi] = pcur[j].pos[headj];
(pcur[i].num)++;
(pcur[j].num)--;
if((pcur[j].num)==0)
{
numtemp = 0;
ppre = pcur;
pcur = (mystack*)malloc(sizeof(mystack)*52);
if(ppre == NULL)
printf("here");
for(k=0;k<curPile;k++)
{
if(ppre[k].num)
{
stackcpy(pcur+numtemp,ppre+k);
numtemp++;
}
}
curPile = numtemp;
free(ppre);
}
return 1;
}
}
}
}
return 0;
}
int main()
{
int i,j;
int k;
int htemp;
int dealed;
int first = 1;
int lastPile;
#ifdef DEBUG
freopen("For Uva.txt","r",stdin);
#endif
while(scanf("%s",input[0]) && input[0][0]!='#')
{
curPile = 1;
pcur = (mystack*)malloc(sizeof(mystack)*52);
pcur[0].num = 1;
pcur[0].pos[0] = 0;
for(i=1;i<52;i++)
{
scanf("%s",input[i]);
pcur[i].num = 0;
}
/** deal then update **/
for(i=1;i<52;i++)
{
dealed = 1;
lastPile = curPile;
for(j=0;j<curPile;j++)
{
if((j+3==lastPile) || (j+1==lastPile))
{
htemp = pcur[j].num-1;
if((input[i][0] == input[pcur[j].pos[htemp]][0]) || (input[i][1] == input[pcur[j].pos[htemp]][1]))
{
pcur[j].pos[++htemp] = i;
(pcur[j].num)++;
dealed = 0;
break;
}
}
}
if(dealed)
{
pcur[curPile].num = 1;
pcur[curPile].pos[0] = i;
curPile++;
}
/** update **/
while(update());
}
if(curPile>1)
printf("%d piles remaining:",curPile);
else
printf("%d pile remaining:",curPile);
for(i=0;i<curPile;i++)
printf(" %d",pcur[i].num);
free(pcur);
puts("");
}
return 0;
}