#include<stdio.h>
typedef struct CARD
{
char rank;
char suit;
}CARD;
bool move(CARD card[][52],int top[],int *position)
{
int index=*position;
if(top[index]==-1)
return false;
int temp=index;
int count=0;
while(count!=3&&temp>=1)
if(top[--temp]!=-1)
count++;
if(count==3)
{
if(card[index][top[index]].rank==card[temp][top[temp]].rank||card[index][top[index]].suit==card[temp][top[temp]].suit)
{
top[temp]++;
card[temp][top[temp]].rank=card[index][top[index]].rank;
card[temp][top[temp]].suit=card[index][top[index]].suit;
top[index]--;
*position=temp;
return true;
}
}
temp=index;
count=0;
while(count!=1&&temp>=1)
if(top[--temp]!=-1)
count++;
if(count==1)
{
if(card[index][top[index]].rank==card[temp][top[temp]].rank||card[index][top[index]].suit==card[temp][top[temp]].suit)
{
top[temp]++;
card[temp][top[temp]].rank=card[index][top[index]].rank;
card[temp][top[temp]].suit=card[index][top[index]].suit;
top[index]--;
*position=temp;
return true;
}
}
return false;
}
int main()
{
char lineone[78];
char linetwo[78];
int i,j;
while(gets(lineone))
{
if(lineone[0]=='#')
break;
gets(linetwo);
CARD card[52][52];
int top[52]={0};
for(i=0,j=0;i<78;)
{
card[j][0].rank=lineone[i++];
card[j][0].suit=lineone[i++];
i++;
j++;
}
for(i=0,j=26;i<78;)
{
card[j][0].rank=linetwo[i++];
card[j][0].suit=linetwo[i++];
i++;
j++;
}
int position=1;
while(position!=52)
{
if(!(move(card,top,&position)&&top[position]!=-1))
position++;
}
int total=0;
for(i=0;i<52;i++)
if(top[i]!=-1)
total++;
if(total!=1)
printf("%d piles remaining:",total);
else
printf("%d pile remaining:",total);
for(i=0;i<52;i++)
if(top[i]!=-1)
printf(" %d",top[i]+1);
printf("\n");
}
return 0;
}
UVa 127--``Accordian'' Patience
最新推荐文章于 2021-01-19 17:20:31 发布