``Accordian'' Patience |
You are to simulate the playing of games of ``Accordian'' patience, the rules for which are as follows:
Deal cards one by one in a row from left to right, not overlapping. Whenever the card matches its immediate neighbour on the left, or matches the third card to the left, it may be moved onto that card. Cards match if they are of the same suit or same rank. After making a move, look to see if it has made additional moves possible. Only the top card of each pile may be moved at any given time. Gaps between piles should be closed up as soon as they appear by moving all piles on the right of the gap one position to the left. Deal out the whole pack, combining cards towards the left whenever possible. The game is won if the pack is reduced to a single pile.
Situations can arise where more than one play is possible. Where two cards may be moved, you should adopt the strategy of always moving the leftmost card possible. Where a card may be moved either one position to the left or three positions to the left, move it three positions.
Input data to the program specifies the order in which cards are dealt from the pack. The input contains pairs of lines, each line containing 26 cards separated by single space characters. The final line of the input file contains a # as its first character. Cards are represented as a two character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen, K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades).
One line of output must be produced for each pair of lines (that between them describe a pack of 52 cards) in the input. Each line of output shows the number of cards in each of the piles remaining after playing ``Accordian patience'' with the pack of cards as described by the corresponding pairs of input lines.
QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S 8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS #
6 piles remaining: 40 8 1 1 1 1 1 pile remaining: 52
移动纸牌的规则:每次移动最左边的,如果可以移动三格或一格就移动三个一直移动到不能移动为止;
模拟题,稍微有点繁琐,只要记录每堆纸牌的数量每一张的花色大小,然后按规则移动后记录,并且把空出来的地方所有后面纸牌前移补上,开始没注意这个问题样例都过不了。
#include <stdio.h>
struct node
{int num;
char rank[53],suit[53];
}a[53];
int max;
int ok(int x,int y)
{if (a[x].rank[a[x].num]==a[y].rank[a[y].num]) return 1;
if (a[x].suit[a[x].num]==a[y].suit[a[y].num]) return 1;
return 0;
};
void move(int x,int y)
{int i,j,ii;
++a[y].num;
a[y].rank[a[y].num]=a[x].rank[a[x].num];
a[y].suit[a[y].num]=a[x].suit[a[x].num];
--a[x].num;
if (a[x].num==0)
{
for (i=x;i<max;i++)
{a[i].num=a[i+1].num;
for (j=1;j<=a[i].num;j++)
{a[i].rank[j]=a[i+1].rank[j];
a[i].suit[j]=a[i+1].suit[j];
}
}
--max;
}
};
int main()
{
int i,j,f,pos,flag;
while (scanf("%c",&a[1].rank[1]))
{if (a[1].rank[1]=='#') break;
scanf("%c",&a[1].suit[1]); a[1].num=1;
for (i=2;i<=52;i++)
{scanf(" %c%c",&a[i].rank[1],&a[i].suit[1]); a[i].num=1;}
f=1; max=52;
while (f)
{f=0; flag=1;
pos=2;
while (flag&&pos<=max)
{if (flag&&(pos>3)&&(ok(pos,pos-3))) {flag=0; move(pos,pos-3);}
if (flag&&(ok(pos,pos-1))) {flag=0; move(pos,pos-1);}
++pos;
}
if (flag==0) f=1;
}
if (max==1) printf("1 pile remaining: 52\n");
else
{printf("%d piles remaining:",max);
for (i=1;i<=max;i++)
printf(" %d",a[i].num);
printf("\n");
}
getchar();
}
return 0;
}