"Accordian" Patience UVA

来源:互联网 发布:马鞍山网络大学招聘 编辑:程序博客网 时间:2024/06/08 15:28

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: ”。接下来从左至右输出各叠牌的数量)

牌堆用栈表示,所有牌堆储存在一个vector里
删除元素用到了vector的erase函数,也可用链表代替vector?

#include <iostream>using namespace std;#include <stack>          //既要移动元素 又要删除空间的结合vector or list 和stack起来使用#include <vector>#include <cstring>struct CARD{    char a;    char b;    CARD(char c, char d) :a(c), b(d){}};bool judge(CARD c1,CARD c2){    if (c1.a == c2.a || c1.b == c2.b)        return true;    return false;}int main(){    char s[3];    int i = 0;      vector<stack<CARD>> cards;    bool remove = true;    while (cin >> s && s[0] != '#')    {           stack<CARD> sta;  //在哪里定义很重要。。        CARD c(s[0], s[1]);        sta.push(c);        cards.push_back(sta);        if (cards.size() == 52)        {            while (remove)            {                remove = false;                for (i = 0; i < cards.size(); i++)                {                    if (i > 2 && judge(cards[i].top(), cards[i - 3].top()))                    {                        cards[i - 3].push(cards[i].top());                        cards[i].pop();                        remove = true;                        if (cards[i].empty())                            cards.erase(cards.begin() + i);                        break;                    }                    if (i > 0 && judge(cards[i].top(), cards[i - 1].top()))                    {                        cards[i - 1].push(cards[i].top());                        cards[i].pop();                        remove = true;                        if (cards[i].empty())                            cards.erase(cards.begin() + i);                        break;                    }                }            }            if (cards.size() == 1)                printf("%d pile remaining:", cards.size());            else                printf("%d piles remaining:", cards.size());            for (i = 0; i < cards.size(); i++)                {                    printf(" %d", cards[i].size());                }            printf("\n");            remove = true;            cards.clear();        }    }    return 0;}
0 0