uva127-模拟纸牌游戏

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

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).

Output

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.

Sample Input

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
#

Sample Output

6 piles remaining: 40 8 1 1 1 1
1 pile remaining: 52
诶可能由于心理恐惧。。。英文好长,一开始就没弄明白这是在干嘛,后面搜了下别人的翻译说是一道模拟题,不过我还是不知道怎么模拟,然后又看了好几个人的方法,最后算是弄明白了,就是让纸牌从左到右平铺,每一堆牌的第一张可以任意移动,一张牌如果与其左边第一个位置或者第三个位置的颜色或者面值一样则可以将此张牌覆盖到左边那张牌,如此类推,最后问还剩几堆牌,每堆有多少张牌,题目其实意思还是挺简单的,可能由于我比较弱,不知道如何来实现,所以只能先看看别人的解题思路了,我选取了最简洁的一个重新按自己的理解打了一遍,其中还知道了scanf()函数在读入的时候面对空格还是有所影响的,代码如下:
#include
#include
#include
#define LOCAL
struct Card{
   char f,s;
}card[55][55];//开一个二维结构体数组用来表示初始的52张牌的每一堆,这便是我没想到的,哎 
int n;
int a[55];
void move(int x){
    for(int i=x;i=3&&(card[i-3][a[i-3]].f==card[i][a[i]].f||card[i-3][a[i-3]].s==card[i][a[i]].s))//要优先考虑第三个位置的 
                {    card[i-3][++a[i-3]]=card[i][a[i]--];flag=true; }//这里进行牌面覆盖 
                if(!flag)
                if(card[i-1][a[i-1]].f==card[i][a[i]].f||card[i-1][a[i-1]].s==card[i][a[i]].s)//与其相距为1的位置 
                {    card[i-1][++a[i-1]]=card[i][a[i]--];flag=true; }
                if(a[i]==-1)move(i);//如果有一堆已经没有牌了,则需要把后面的往前移动以便统计 
                if(flag)break;
            }
            if(!flag)break;
        }
        if(n==1)printf("1 pile remaining:");
        else printf("%d piles remaining:",n); 
        for(int i=0;i

你可能感兴趣的:(uvaoj,模拟)