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 (“手风琴”纸牌),布布扣,bubuko.com

时间: 2024-11-25 00:34:12

UVa OJ 127 - "Accordian" Patience (“手风琴”纸牌)的相关文章

uva 127 &quot;Accordian&quot; Patience(手风琴纸牌)

用 栈 stack 来处理. 直接根据题目描述写就可以.不要忘记每组数据最后的清空栈堆. 题目大意: 给定52张的扑克牌,现在要求对扑克牌进行整理,对于每一张扑克牌,如果左边的第三张存在那么就去判断这一张是否和第三张满足花色或卡片值相同,如果满足则把这一张移动到左边的第三张,否则去判断左边的第一张是否和这一张满足条件:如果左边的第三张不存在那么只要去判断左边的第一张即可.最后输出剩下的扑克牌的堆数,并且输出每一堆的牌数. #include<stdio.h> #include<iostre

UVa 127 - &quot;Accordian&quot; Patience POJ 1214 链表题解

UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比较简单,这里纯粹使用指针做了,非常麻烦的指针操作,一不小心就错.调试起来还是非常费力的 本题理解起来也是挺费力的,要搞清楚如何模拟也不容易啊,读题要很仔细. 纯指针的操作挺快的吧.不过POJ 0ms,而UVa就0.2左右了. 三相链表: 1 只要有叠起来的牌,那么就使用一个down指针指向下面的牌就可以了. 2 使用双向链表,可以方便前后遍历. 3 记得有了更新牌之后,又要重新开始检查是否需要更新牌,这是

ACM学习历程——UVA 127 &quot;Accordian&quot; Patience(栈;模拟)

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

UVA 127 &quot;Accordian&quot; Patience

题意: 按从左至右的顺序发牌,并摆成一行,发牌不要相互重叠.游戏中一旦出现任何一张牌与它左边的第一张或第三张“匹配”,即花色或点数相同,则须立即将其移动到那张牌上面.如果牌被移动后又出现了上述情况,则需再次向左移动.每叠牌只能移动最上面的一张.如果一叠牌被移空,应该立即将右边各叠整体向左移动,补上这个空隙.依次将整副牌都发完,并不断的向左合并.如果全部移动结束后整副牌都放成了一叠,则游戏胜利. 分析: 用sum表示有多少个牌挪动了,最后输出52-sum.判断是否能放到左边第三张的时候需要注意再前

127 - &quot;Accordian&quot; Patience

很简单呢的一道题,定义了一个结构体数组,用vector来做为一个牌堆.将尾部作为牌堆的顶部. 要注意,当最后只剩一堆牌的时候,答案中的pile不加s #include<bits/stdc++.h> using namespace std; const int maxn = 60; int b[60]; struct pile { vector<string> a; }; int main() { ios::sync_with_stdio(false); string buf; wh

&quot;Accordian&quot; Patience UVA 127 (”手风琴“牌游戏)

"Accordian" Patience From:UVA, 127 Submit Time Limit: 3000 MS 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 th

``Accordian&#39;&#39; Patience UVA 127

说说: 这道题难度其实并不但,但是比较繁琐,且细节容易被忽略.先分析一下游戏规则吧,知道游戏规则之后,问题自然而然就解决了.首先放着一行52个扑克牌堆(ps:输入的时候分两行输入)开始每堆只有一张牌,然后从左到右开始判断,若一张牌和左边第一张牌或者左边第三张牌的大小或者花色相同,则将该张牌放到那一对牌之上并且要求继续向左匹配,直到不能匹配为止.若某个堆一张牌都不剩了,则该堆不存在了,也就是说如果两堆相邻,则两堆的牌数都不能为0.最后按照这个规则,直到没有牌能够移动位置.(ps:移动是指顶层牌的移

Uva 127 poj 1214 `Accordian&#39;&#39; 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

uva ``Accordian&#39;&#39; 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