【UVaOJ】127 - "Accordian" 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 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 betweenthem 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


思路:

      从左至右依次扫描每一张牌,然后将牌根据根则移到所能移动到的最左端的位置。移动完一张牌之后,再判断该牌右边牌中是否存在可移动的牌。


C++代码实现

#include <cstdio>
#include <list>
#include <vector>

using namespace std;

struct Card;
typedef list<vector<Card> >::iterator  list_iterator;

struct Card {
    char rank;
    char suit;
    Card(char _rank, char _suit) : rank(_rank), suit(_suit) {}
};

bool operator== (const Card &a, const Card &b) 
{
    return a.rank == b.rank || a.suit == b.suit;
}

list_iterator ThirdLeft(list_iterator it)
{
    --it; --it; --it;
    return it;
}

list_iterator Neighbour(list_iterator it)
{
    return --it;
}

int main(int argc, char *argv[])
{	
   // freopen("in.txt","r",stdin);
    
    char card[4];
    list<vector<Card> > cards_list;
    while (scanf("%s",card) != EOF && card[0] != '#') {
        cards_list.clear();
        
        // 将第一个牌保存 
        cards_list.push_back(vector<Card>(1, Card(card[0],card[1])));
        
        // 读取剩下的51张牌 
        for (int i = 0; i < 51; ++i) {
            scanf("%s", card);
            cards_list.push_back(vector<Card>(1,Card(card[0],card[1])));
        }
        
        auto current = cards_list.begin();
        auto real_begin = current;
        auto third_left = current;
        auto neighbour = current;
        auto leftest = current;    // 当前牌所能到达的最左端位置 
        
        // 加入三个哨兵 
        cards_list.push_front(vector<Card>(1,Card(0,0)));
        cards_list.push_front(vector<Card>(1,Card(0,0)));
        cards_list.push_front(vector<Card>(1,Card(0,0)));
        
        for (++current; current != cards_list.end(); ++current) {
            leftest = current;
            third_left = ThirdLeft(leftest);
            neighbour = Neighbour(leftest);
            Card val = current->back();
            
            // 寻找所能移动到的最左的位置 
            while (third_left->back() == val || neighbour->back() == val) {
                if (third_left->back() == val) {
                    leftest = third_left;
                } else {
                    leftest = neighbour;
                }
                third_left = ThirdLeft(leftest);
                neighbour = Neighbour(leftest);
            }
            
            if (leftest != current) {
                leftest->push_back(val);
                current->pop_back();
                if (current->size() == 0) { // 消除gap 
                    cards_list.erase(current);
                }
                current = leftest; // 下次迭代从最当前牌位置的右边开始 ,判断是否存在可移动的牌 
            }
        }
        
        // 删除三个哨兵
        cards_list.erase(cards_list.begin(),real_begin); 
        
        // 输出结果 
        int n = cards_list.size();
        printf("%d %s remaining:", n, n > 1 ? "piles" : "pile");
        for (auto it = real_begin; it != cards_list.end(); ++it) {
            printf(" %d", it->size());
        }
        printf("\n");
    }
	return 0;
}


你可能感兴趣的:(list,127,UvaOJ)