• Uva 127 "Accordian" Patience


    ``Accordian'' Patience 

    Time limit: 3.000 seconds

    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

     

     

    #include<stdio.h>
    #include<string.h>
    
    typedef struct {
        char fvalue;
        char suit;
    }Card;
    
    typedef struct{
        Card pile[56];
        int count;
    }Pile;                  //Pile记录一堆卡牌,count记录一堆中有多少卡牌 
    
    Pile piles[56];         //deal前所有卡牌堆数和顺序 
    int n;                  //卡牌的堆数 
    
    int delectpile(int i)
    {//删除无卡牌的堆的位置,将后面的卡堆向前移 
        int j;
        for(; i<n; ++i)
        {
            for(j=0; j<=piles[i+1].count; ++j)
            {
                piles[i].pile[j].fvalue = piles[i+1].pile[j].fvalue;
                piles[i].pile[j].suit = piles[i+1].pile[j].suit;
            }
            piles[i].count = piles[i+1].count;
        }
        n--;
        return 0;
    }
    
    
    int main()
    {
         
        char incard[3], ivalue, isuit;
        int i, flag, never; 
        while(1)
        {
            n = flag = 0;
            for(i=0; i<52; ++i)
            {
                scanf("%s", incard);
                if(incard[0] == '#') {flag = 1; break;}
                ivalue = incard[0], isuit = incard[1];
                piles[i].pile[0].fvalue = ivalue;
                piles[i].pile[0].suit = isuit;
                piles[i].count = 0;
                n++;
            }
            if(flag) break;     //"#"号表示结束 
            while(1)
            {
                never = 0;
                for(i=1; i<n; ++i)
                {
                    if(i>=3)
                    {
                        if(piles[i-3].pile[piles[i-3].count].fvalue == piles[i].pile[piles[i].count].fvalue || piles[i-3].pile[piles[i-3].count].suit == piles[i].pile[piles[i].count].suit)
                        {
                            piles[i-3].pile[++(piles[i-3].count)].fvalue = piles[i].pile[(piles[i].count)].fvalue;
                            piles[i-3].pile[(piles[i-3].count)].suit = piles[i].pile[(piles[i].count)--].suit;
                            never = 1;
                        }
                    }
                    if(!never)    //如果移动到了前第三个这里的判断决定了要不要移动到第二个位置 
                    if(piles[i-1].pile[piles[i-1].count].fvalue == piles[i].pile[piles[i].count].fvalue || piles[i-1].pile[piles[i-1].count].suit == piles[i].pile[piles[i].count].suit)
                    {
                        piles[i-1].pile[++(piles[i-1].count)].fvalue = piles[i].pile[(piles[i].count)].fvalue;
                        piles[i-1].pile[(piles[i-1].count)].suit = piles[i].pile[(piles[i].count)--].suit;
                        never = 1;
                    }
                    if(piles[i].count == -1)
                    {
                        delectpile(i);
                        break;
                    }
                    if(never) break;
                }
                if(!never) break;   //无可再移动的卡牌,说明deal结束 
            }
            
            if(n==1) printf("1 pile remaining: %d\n", piles[0].count+1);
            else 
            {
                printf("%d piles remaining:", n);
                for(i=0; i<n; ++i)
                printf(" %d", piles[i].count+1);
                printf("\n");
            }
        }
        return 0;
    }

    解题报告:

    这题并不需要用到数据结构里的东西,简单的模拟,主要是看懂题意,注意题目所描述的情况,比如说可以移动到前第三位置和前第一位置时应该移动到前第三位置,而这时又需要从头开始遍历,主要是为了看刚才移动后改变的牌面是否有符合移动的卡牌。

    非技术性的东西:

    一开始看到题目时,就被很长很长的英文下着了,一遍浏览过去后,再仔细的看了两遍,发现还没看懂题意,然后根据自己对题意的理解,对着Sample自己推了一下,推到一半的时候发现这仅仅是计算机才能做到的事情,浪费了大半的时间,这不值得!无奈之下百度一下最终才理解了题意,但发现自己理解了还是很难动手写代码,我打算“deal”一张牌后判断是否需要向前移,最糟糕的情况是(1+52)*52/2,按理来说应该是可以的,但考虑到还要重新遍历,最终还是选择了一次性输入,然后考虑如何处理。

    Debuge: 在赋值到前一或三的时候,连用了两次++和--,而情况只需要一次,找了很长时间的错误





  • 相关阅读:
    [转载]java日志框架log4j详细配置及与slf4j联合使用教程
    log4j的正确使用姿势
    eclipse 安装properties编辑器,显示中文
    tips:解决bootstrap-switch 在jqgrid中动态加载不显示的问题
    jqgrid 设置为每行单选
    MySQL 批量修改某一列的值为另外一个字段的值
    MySQL 使用左连接替换not in
    分析一个react项目
    React不同版本之间需要注意的地方
    一个出色的表格(React实现__ES5语法)
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2793510.html
Copyright © 2020-2023  润新知