/*注意每次移动后都从头开始!*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int top[52];
char stack[52][52][3];
int Match(char a[],char b[])
{
if(a[0]==b[0]||a[1]==b[1])return 1;
return 0;
}
int find(int i,int n)
{
int j,tag;
tag=0;
for(j=i-1;j>=0;j--)
{
if(top[j]>=0)
{
tag++;
}
if(tag==n)break;
}
if(j>=0)return j;
else return -1;
}
int Play(char a[][3])
{
int i,j,k,count=0,t;
for(i=0;i<52;i++)
{
++top[i];
strcpy(stack[i][0],a[i]);
}
for(i=1;i<52;i++)
{
if(i>2&&top[i]>=0)
{
t=find(i,3);
if(t>=0)
{
if(Match(stack[i][top[i]],stack[t][top[t]]))
{
++top[t];
strcpy(stack[t][top[t]],stack[i][top[i]]);
top[i]--;
i=0;
continue;
}
}
}
if(top[i]>=0)
{
t=find(i,1);
if(t>=0)
{
if(Match(stack[i][top[i]],stack[t][top[t]]))
{
++top[t];
strcpy(stack[t][top[t]],stack[i][top[i]]);
top[i]--;
i=0;
}
}
}
}
for(i=0;i<52;i++)
{
if(top[i]>=0)count++;
}
return count;
}
int main()
{
int i,j,k,pile,t;
char pork[52][3],c;
i=j=0;
while((c=getchar())&&c!='#')
{
if(isalpha(c)||isdigit(c))
{
pork[i][j++]=c;
}
else
{
pork[i][2]='\0';
i++;
j=0;
}
if(i==52)
{
for(k=0;k<52;k++)
{
top[k]=-1;
}
i=0;
j=0;
pile=Play(pork);
if(pile==1)printf("%d pile remaining:",pile);
else printf("%d piles remaining:",pile);
for(k=0;k<52;k++)
{
if(top[k]>=0)printf(" %d",top[k]+1);
}
printf("\n");
}
}
return 0;
}
UVA - 127 - "Accordian" Patience
最新推荐文章于 2022-02-25 19:38:17 发布