uva 127 ``Accordian'' Patience(栈+模拟)

来源:互联网 发布:小学手机游戏编程培训 编辑:程序博客网 时间:2024/06/05 14:57

 ``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<iostream>#include<stack>using namespace std;#define N 5#define M 52struct st{stack<char> n;stack<char>c;st *p;}*head=NULL,*move=NULL;//head一直指向头部,move用于遍历。st* find(int n);//该函数用于寻找当前第n个牌堆,返回牌堆。int main(){ char str[N];int i;while(cin>>str,str[0]!='#'){//Init.head=new st;head->n.push(str[0]);head->c.push(str[1]);head->p=NULL;move=head;//Read.for(i = 1;i < 52; i++){cin>>str;move->p=new st;move=move->p;move->n.push(str[0]);move->c.push(str[1]);move->p=NULL;}//Make.move=head->p;int t=1;while(move!=NULL)//合并。{st *f=find(t-3);//找到左边第三张。st *e=find(t-1);//找到左边第一张。if(f == NULL);//没有的时候跳过。else if(f->n.top() == move->n.top()||f->c.top() == move->c.top()){f->n.push(move->n.top());//转移扑克。f->c.push(move->c.top());move->n.pop();//去除第一张。move->c.pop();if(move->c.empty())//链表的删除过程。{e->p=move->p;delete move;}move=head->p;//进行一次移动后,从头进行考虑。t=1;continue;}if(e == NULL);else if(e->n.top() == move->n.top()||e->c.top() == move->c.top()){e->n.push(move->n.top());//转移扑克。e->c.push(move->c.top());move->n.pop();//去除第一张。move->c.pop();if(move->c.empty())//链表的删除过程。{e->p=move->p;delete move;}move=head->p;//进行一次移动后,从头进行考虑。t=1;continue;}move=move->p;t++;}move=head;for(i = 0;move!=NULL;i++)//计算牌堆数。move=move->p;if(i==1)cout<<i<<" pile remaining:";elsecout<<i<<" piles remaining:";for(move=head;move!=NULL;move=move->p)cout<<" "<<move->c.size();cout<<endl;}return 0;}st* find(int n){int i;st *f=head;if(n < 0)return f=NULL;for(i = 0;i < n; i++)f=f->p;return f;}


原创粉丝点击