UVa OJ 127 - "Accordian" Patience (“手风琴”纸牌)

UVa OJ 127 - "Accordian" Patience (“手风琴”纸牌)

Time limit: 3.000 seconds
限时:3.000秒

 

Problem
问题

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).
输入到程序中的数据指定了一副牌的顺序。每组输入包括两行,每行26张牌,牌与牌之间用空格隔开。以#号开头的一行作为输入的结束行。每张牌由两个字符表示,第一个字符是点数:(A、 2-9、T=10、J、Q, K);第二个字符是花色:(C=梅花、D=方块、H=红心、S=黑桃)。

 

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.
每两行输入(定义了一副52张牌的顺序)必须产生一行输出。每行输出包括剩下的叠数以及每叠中的牌数。

 

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

(译注:第一个数字为剩下的叠数,当叠数为1时,后面输出“pile remaining: ”,否则输出“piles remaining: ”。接下来从左至右输出各叠牌的数量)

 

Analysis
分析

这两天UVa OJ的服务器老是不稳定,提交的解答一直“In judge queue”,不得不停工了很长时间!下面的代码用几百组随机数据测试过,与UVa Toolkit提供的所谓正确答案比对,结果是完全相同的。不管怎样思路应该不会错,所以先贴上来,也好清空代码做下一题。如果OJ出来没有AC,再修改也不迟。(更新:OJ出来了,AC,0.772秒)

解这道题前务必认真的理解题意,一点小误差都会引起算法的区大差别。最好先拿副扑克牌先玩几遍,熟练的过程中思路自然就产生了,还可能会发现一些技巧。

总体还是采用步进迭代的方法来解,每发一张牌就执行所有可能的移动,然后再发下一张。直到所有的牌都发完,统计并输出结果。存储牌叠应采用二维动态数组,我使用的是两层vector,如果追求效率可将外层的vector改为list,但实现的代码会比较复杂。

按照题目要求,每次移动一张牌时,应一次性向左移动到再也不能移动的位置上。此时这张牌的右边就可能再次出现可以移动的牌。因此,发牌后可以执行的操作是:将一张牌(新发的牌应放在最后作为第一次移动的牌)向左移动完毕后,向右查找第一个可以移动的牌;找到后再向左移动,然后再从移动后的位置向右查找,以此类推,直到再也找不到可以移动的牌为止。

用三重循环来解决问题,最外层的循环就是按顺序发牌。每次发一张牌就将这张牌放到最后作为新的一叠。中间一层循环用来向右遍例每一张可以移动的牌,找到后就向左移动。最内的循环就是查找当前牌可以移动到的位置(最左边的)。

从该题的Statics来看,OJ给出的测试数据量貌似比较大,因此要比较小心的实现。为了加快运算,我没有使用特别的数据结构来转存牌面,这也是没有必要的,完全可以用原输入的字符进行处理。CARD结构体只是用于表示两个字符的指针。

这道题本身不难,图释就划不来了,还是看代码吧。

 

Solution

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
struct card
{
    char number;
    char color;
    card(char _number=' ',char _color=' '):number(_number),color(_color){}
};
vector<vector<card> > vec;

int  deal(card cc,int length)   //该函数就是用来判断,卡牌根据游戏的规则,最后究竟移到什么地方
{

    int temp;
  //  int length=vec.size();
    while(length>=0)
    {
        if(length-3>=0)
        {
            temp=vec[length-3].size()-1;
            if(vec[length-3][temp].number==cc.number||vec[length-3][temp].color==cc.color)
            {
                length-=3;
                continue;
            }
        }
        if(length-1>=0)
        {
            temp=vec[length-1].size()-1;
            if(vec[length-1][temp].number==cc.number||vec[length-1][temp].color==cc.color)
            {
                length-=1;
                continue;
            }
        }
        break;
    }
    return length;     //返回移动到的地方
}
int main()
{
    //freopen("a.txt","r",stdin);
    //ofstream fcout("b.txt");
    char inputs[3];
    unsigned int i;
    while(cin>>inputs&&inputs[0]!='#') //输入的数据
    {

        int number=52;
        while(number--)   //一组数据,包含52个卡牌
        {
            card cc(inputs[0],inputs[1]);
            int length=deal(cc,vec.size());
            if(length==(int)vec.size())  //卡片不能移动,直接插入
            {
                vector<card> vv(1,cc);
                vec.push_back(vv);
            }
            else               //卡片能够移动,length就是最后应该放入的地方
            {
               vec[length].push_back(cc);
               TT: for(i=length+1;i<vec.size();i++)  //卡片插入之后,插入点后面的卡片也有可能移动
                {                                       //循环遍历后面所有的点找到插入的地方
                    card dd=vec[i][vec[i].size()-1];
                    unsigned int new_location=deal(dd,i);
                    if(new_location!=i)
                    {
                         vec[new_location].push_back(dd);
                         vec[i].pop_back();
                         if(vec[i].size()==0)
                            vec.erase(vec.begin()+i);
                         length=new_location;
                         goto TT;

                    }
                }
            }
            if(number==0)
                continue;
            cin>>inputs;
        }
        cout<<vec.size()<<(vec.size()!=1?" piles remaining: ":" pile remaining: ");
        for(i=0;i<vec.size();i++)
        {
            if(i!=vec.size()-1)
                cout<<vec[i].size()<<" ";
            else
                cout<<vec[i].size()<<endl;
        }
        vec.clear();
    }

    return 0;
}


你可能感兴趣的:(UVa OJ 127 - "Accordian" Patience (“手风琴”纸牌))