uva ``Accordian'' Patience

题目如下:

``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

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


中等难度的模拟题,看懂题后直接模拟即可,这道题好的是永远只能往左移并且永远是最左边的先移,尽量移到最左边(好绕QAQ),少去了很多细节。用一个二维数组记录每张牌的位置,再用一个num数组记录每个位置牌的数目,移动之后注意回溯。刚开始回溯时从最开始回溯,结果超时了,最后改为从新位置回溯并且当那个位置牌的数目不为0时再操作,节约了不少时间,成功AC了。设置三个函数,一个用于移动,一个用于向左移三位,一个用于向左移一位。

AC的代码如下:

#include
   
   
    
    
#include
    
    
     
     
struct card
{
    char val;
    char suit;
};
int num[55]= {0};
card ca[55][55];
void Move(int x,int y)
{
    num[y]++;
    ca[y][num[y]-1]=ca[x][num[x]-1];
    num[x]--;
}
int isleftthree(int x)
{
    int i,Count=0,ok=0;
    for(i=x-1; i>=0; i--)
    {
        if(num[i]!=0)
            Count++;
        if(Count==3)
        {
            ok=1;
            break;
        }
    }
    if((ok==1)&&(ca[i][num[i]-1].val==ca[x][num[x]-1].val||ca[i][num[i]-1].suit==ca[x][num[x]-1].suit))

    {
        Move(x,i);
        return i;
    }

    else
        return 0;

}
int isleftone(int x)
{
    int i,Count=0,ok=0;
    for(i=x-1; i>=0; i--)
    {
        if(num[i]!=0)
            Count++;
        if(Count==1)
        {
            ok=1;
            break;
        }
    }
    if((ok==1)&&(ca[i][num[i]-1].val==ca[x][num[x]-1].val||ca[i][num[i]-1].suit==ca[x][num[x]-1].suit))
    {
        Move(x,i);
        return i;
    }
    else
        return 0;
}


int main()
{

    char s[3];
    int i=3;
    while(scanf("%s",s)!=EOF)


    {
        if(s[0]!='#')

        {
            num[i]++;

            ca[i][num[i]-1].val=s[0];
            ca[i][num[i]-1].suit=s[1];

            if(i==54)
            {
                for(i=0; i<=2; i++)
                    ca[i][num[i]].val=ca[i][num[i]].suit='\0';

                for(i=3; i<=54; i++)
                {
                    int ok=0,re;
                    if(num[i]!=0)
                    {
                        re=isleftthree(i);
                        if(re)
                            ok=1;
                        else
                        {
                            re=isleftone(i);
                            if(re)
                                ok=1;
                        }
                        if(ok==1)
                            i=re-1;
                    }

                }
                int co=0;
                for(i=3; i<=54; i++)
                    if(num[i]!=0)
                        co++;
                if(co>1)
                    printf("%d piles remaining:",co);
                else
                    printf("%d pile remaining:",co);
                for(i=3; i<=54; i++)
                    if(num[i]!=0)
                        printf(" %d",num[i]);
                printf("\n");

                i=2;
                memset(num,0,sizeof(num));
                for(int k=0; k<=54; k++)
                    for(int m=0; m<=54; m++)
                        ca[k][m].suit=ca[k][m].val='\0';
            }
        }
        else
            break;
        i++;
    }
    return 0;
}


    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值