现在的位置: 首页 > 综合 > 正文

127 – “Accordian” Patience

2018年01月12日 ⁄ 综合 ⁄ 共 3118字 ⁄ 字号 评论关闭

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

这几天在看数据结构,效率极低。。

题意:有一副牌,52张,按照从左到右的顺序发牌,当该牌与左边第一张或者左边第三张能够匹配的时候,就将其放到左侧对应的上面。(当两张牌的点数或是花色相同,就为匹配)(如果左边第一张和第三张都符合要求,则放置第三张)

如果移到相应的牌上面之后,还能继续移动,则移动到无法移动位置。如果原先放牌的位置为空了,就将右侧所有的牌向左平移。知道所有的都无法移动,求出一共有多少堆

一开始打算用链表做,可是链表还得是双向链表。而且移动起来也挺麻烦。后来因为总共也就52张牌,就直接开了个栈的数组。然后模拟牌的移动就可以了。如果出现空栈的情况,就将后面的数组填补过来

要注意到最后要把所有的栈都清空,第一次做完就是因为没有清空,导致后面几组就有问题。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdlib>
#include<ctype.h>
#include<string>
#define maxn 60
using namespace std;
stack<string> s[maxn];
bool match(int a, int b)
{
    string A,B;
    if (s[a].top()[0]==s[b].top()[0] || s[a].top()[1]==s[b].top()[1]) return true;
    return false;
}
int main ()
{
    string str;
    int i,j,n=0;
    while(cin>>str)
    {
        if (str=="#") break;
        s[n++].push(str);
        while(n<52)
        {
            cin>>str;
            s[n++].push(str);
        }
        i=1;
        while(i<n)
        {
            string temp;
            int flag=0;
            if (i>=1 && match(i,i-1)) flag=1;
            if (i>=3 && match(i,i-3))
            {
                flag=2;
            }
            if (flag==1)
            {
                temp=s[i].top();
                s[i].pop();
                s[i-1].push(temp);
                if (s[i].empty())
                {
                    if (i<n-1)
                    {
                        for (j=i; j<n-1; j++)
                            s[j]=s[j+1];
                    }
                    n--;
                }
                i=i-2;
            }
            else if (flag==2)
            {
                temp=s[i].top();
                s[i].pop();
                s[i-3].push(temp);
                if (s[i].empty())
                {
                    if (i<n-1)
                    {
                        for (j=i; j<n-1; j++)
                            s[j]=s[j+1];
                    }
                    n--;
                }
                i=i-4;
            }
            i++;
        }
        cout<<n<<" ";
        if (n==1) cout<<"pile remaining: ";
        else cout<<"piles remaining: ";
        for (i=0; i<n-1; i++)
        {
            cout<<s[i].size()<<" ";
        }
        cout<<s[n-1].size()<<endl;
        for (i=0; i<52; i++)
        {
            while(!s[i].empty())
            {
                s[i].pop();
            }
        }
        n=0;
    }
    return 0;
}

抱歉!评论已关闭.