uva 127 ``Accordian'' Patience

来源:互联网 发布:全民淘宝客 编辑:程序博客网 时间:2024/06/05 02:37

                             uva 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 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 6S8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5CAC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KDAH 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 11 pile remaining: 52

题目大意:一份扑克,52张牌(分两行),从左到右排列,输入‘#’为结束。扑克从左到右考虑,对于每张牌,如果左边第三牌或左边第一张(优先考虑左边第三张)有花色或数字一样,就将该牌移动到那张牌的上面。

输出最后剩余的牌堆数,并输出每个牌堆的扑克牌数。

注:1、移动后的牌堆,只考虑最上面一张,只有上面一张移动后,下面一张才能出现,并且要考虑它的移动。
    2、每进行一次移动后都需从头再来。   
    3、如果剩余牌堆为1,注意不可以加s,格式很重要。

解题思路:用一个结构体储存扑克牌的数字和花色,再定义一个结构体作为链表节点(扑克牌结构体的栈,指向下一个节点的指针),将扑克牌按顺序链成一个链表,然后进行模拟,最后输出结果。


#include<stdio.h>#include<stack>using namespace std;struct poc {        //扑克牌的结构体    char value;    char suit;    poc () {}    poc (char a,char b) {        value = a;        suit = b;    }};struct link {         //链表节点结构体    stack<poc> pocc;    link *next;};link *first;link *find (int i) {        //单链表不能往回走,find函数实现链表回头    if (i < 0) return NULL;    link *t = first;    for (int j = 0; j < i; j++) {        t = t -> next;    }    return t;}int main() {    while (true) {        first = new link;        char card[3];        scanf("%s", card);        if (card[0] == '#') break;            //遇到 # 结束        first -> pocc.push(poc(card[0],card[1]));        link *temp = NULL;        temp = first;        for (int i = 1; i < 52; i++) {            temp -> next = new link;            temp = temp -> next;            scanf("%s", card);            temp -> pocc.push(poc(card[0], card[1]));        }        int cnt = 1;        temp = first -> next;        while (temp != NULL) {            //进行模拟            link *l1 = find(cnt - 3);            link *l2 = find(cnt - 1);            if (l1 != NULL && (l1 -> pocc.top().value == temp -> pocc.top().value || l1 -> pocc.top().suit == temp -> pocc.top().suit))     {                l1 -> pocc.push(temp -> pocc.top());                temp -> pocc.pop();                if (temp -> pocc.empty()) {                    l2 -> next = temp -> next;                    delete temp;                }                temp = first -> next;                cnt = 1;         //扑克牌顺序发生改变,需要重头模拟                continue;            }            else if (l2 != NULL && (l2 -> pocc.top().value == temp -> pocc.top().value || l2 -> pocc.top().suit == temp -> pocc.top().suit)) {                l2 -> pocc.push(temp -> pocc.top());                temp -> pocc.pop();                if (temp -> pocc.empty()) {                    l2 -> next = temp -> next;                }                    temp = first -> next;                cnt = 1;                continue;            }            temp = temp -> next;            cnt++;        }        temp = first;        int  i;        for (i = 0; temp != NULL; i++) {            temp = temp -> next;        }        if (i == 1) {             //注意格式            printf("1 pile remaining:");        }        else  {            printf("%d piles remaining:", i);        }        temp = first;        while (temp != NULL) {            printf(" %d", temp -> pocc.size());            temp = temp -> next;        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击