"Accordian" Patience UVA 127 (”手风琴“牌游戏)

"Accordian" Patience

From:UVA,
127

Submit

Time Limit: 3000 MS



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张扑克,从左到右在平面上排列,按着如下规则处理:

1.按照从左到右的顺序,如果一张牌和左边的第一张或者第三张匹配,就把它放到对应的牌上面。

2.如果可以移动到多个位置,移动到最左端的牌上面。(匹配:花色或者数值相同)

注意:每次只移动每叠牌的最顶上的牌。

解题思路:vector /  list、栈、模拟。对于每叠牌建立一个栈,进行模拟即可。

代码一:( list + stack )

#include<iostream>
#include<list>
#include<stack>
#include<cstdio>
using namespace std;

struct card{
    char x,y;
    card(char x0,char y0){x=x0,y=y0;}
};

list< stack<card> >cards;
list <stack<card> >::iterator it;

bool judge(card a,card b){//判断可以调换的情况
    if(a.x==b.x||a.y==b.y) return true;
    return false;
}

list< stack<card> >::iterator pre1(const list< stack<card> >::iterator i ){
    list < stack<card> >::iterator it=i;//函数作用:当左边第一张等时返回左边第一个的地址
    return --it;
}

list <stack<card> >::iterator pre3(const list< stack<card> >::iterator i){
    list <stack<card> >::iterator it=i;//函数作用:当左边第三张等时
    return ------it;//返回前三个的地址
}

bool input(){
    char s[3];
    while(~scanf("%s",s)){
        if(s[0]=='#') return false;
        card c(s[0],s[1]);
        stack<card> temp;//栈是push
        temp.push(c);//列表是push_back
        cards.push_back(temp);
        if(cards.size()==52) return true;
    }
}

void solve(){
    bool ismoved = true;
    while(ismoved){
        ismoved = false;
        size_t cnt;//unsigned int;
        for(it=cards.begin(),cnt=0;it!=cards.end();it++,cnt++){
            if(cnt>2&&judge(it->top(),pre3(it)->top())){
                pre3(it)->push(it->top());
                it->pop();
                ismoved=true;
                if(it->empty()){
                    cards.erase(it);
                }
                break;
            }
             if(cnt>0&&judge(it->top(),pre1(it)->top())){
                pre1(it)->push(it->top());
                it->pop();
                ismoved=true;
                if(it->empty()){
                    cards.erase(it);
                }
                break;
            }
        }
    }
}

void outResult(){
    if(cards.size()==1){
        printf("%d pile remaining:",cards.size());
    }
    else  printf("%d piles remaining:",cards.size());
    for(it=cards.begin();it!=cards.end();it++){
        printf(" %d",it->size());//只能用迭代器访问,不能用下标
    }
    printf("\n");
    cards.clear();
}

int main(){
    while(input()){
        solve();
        outResult();
    }
    return 0;
}

代码二:( vector+stack )

#include<iostream>
#include<vector>
#include<stack>
#include<cstdio>
using namespace std;

struct card{
    char x,y;
    card(char x0,char y0){x=x0,y=y0;}
};

vector< stack<card> >cards;

bool judge(card a,card b){//判断可以调换的情况
    if(a.x==b.x||a.y==b.y) return true;
    return false;
}

int pre1(int i ){
    int it=i;//函数作用:当左边第一张等时返回左边第一个的地址
    return --it;
}

int pre3(int i ){
    int it=i;//函数作用:当左边第三张等时
    return ------it;//返回前三个的地址
}

bool input(){
    char s[3];
    while(~scanf("%s",s)){
        if(s[0]=='#') return false;
        card c(s[0],s[1]);
        stack<card> temp;//栈是push
        temp.push(c);//列表是push_back
        cards.push_back(temp);
        if(cards.size()==52) return true;
    }
}

void solve(){
    bool ismoved = true;
    while(ismoved){
        ismoved = false;
        for(int it=0;it!=cards.size();it++){
            if(it>2&&judge(cards[it].top(),cards[it-3].top())){
                cards[it-3].push(cards[it].top());
                cards[it].pop();
                ismoved=true;
                if(cards[it].empty()){
                    cards.erase(cards.begin()+it);
                }
                break;
            }
            if(it>0&&judge(cards[it].top(),cards[it-1].top())){
                cards[it-1].push(cards[it].top());
                cards[it].pop();
                ismoved=true;
                if(cards[it].empty()){
                    cards.erase(cards.begin()+it);
                }
                break;
            }
        }
    }
}

void outResult(){
    if(cards.size()==1){
        printf("%d pile remaining:",cards.size());
    }
    else  printf("%d piles remaining:",cards.size());
    for(int it=0;it<cards.size();it++){
        printf(" %d",cards[it].size());//只能用迭代器访问,不能用下标
    }
    printf("\n");
    cards.clear();
}

int main(){
    while(input()){
        solve();
        outResult();
    }
    return 0;
}

时间: 2024-10-12 13:10:11

"Accordian" Patience UVA 127 (”手风琴“牌游戏)的相关文章

``Accordian&#39;&#39; Patience UVA 127

说说: 这道题难度其实并不但,但是比较繁琐,且细节容易被忽略.先分析一下游戏规则吧,知道游戏规则之后,问题自然而然就解决了.首先放着一行52个扑克牌堆(ps:输入的时候分两行输入)开始每堆只有一张牌,然后从左到右开始判断,若一张牌和左边第一张牌或者左边第三张牌的大小或者花色相同,则将该张牌放到那一对牌之上并且要求继续向左匹配,直到不能匹配为止.若某个堆一张牌都不剩了,则该堆不存在了,也就是说如果两堆相邻,则两堆的牌数都不能为0.最后按照这个规则,直到没有牌能够移动位置.(ps:移动是指顶层牌的移

&quot;Accordian&quot; Patience (UVa 127) 双向链表 栈

题目:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=63 思路: 从第一个牌堆开始,向右进行遍历.若存在左边第三个牌堆,则优先考虑能否移动到左边第三个牌堆,若不存在或不能移动,再考虑左边第一个牌堆,若都不能移动,则继续考虑下一个牌堆.由于"如果有多张牌可以移动,先移动最左边的牌",因此进行移动操作以后,要从移动到的那张

UVa OJ 127 - &quot;Accordian&quot; Patience (“手风琴”纸牌)

UVa OJ 127 - "Accordian" Patience ("手风琴"纸牌) Time limit: 3.000 seconds 限时:3.000秒 Problem 问题 You are to simulate the playing of games of "Accordian" patience, the rules for which are as follows: 模拟玩一个"手风琴"纸牌游戏,规则如下: D

uva 127 &quot;Accordian&quot; Patience(手风琴纸牌)

用 栈 stack 来处理. 直接根据题目描述写就可以.不要忘记每组数据最后的清空栈堆. 题目大意: 给定52张的扑克牌,现在要求对扑克牌进行整理,对于每一张扑克牌,如果左边的第三张存在那么就去判断这一张是否和第三张满足花色或卡片值相同,如果满足则把这一张移动到左边的第三张,否则去判断左边的第一张是否和这一张满足条件:如果左边的第三张不存在那么只要去判断左边的第一张即可.最后输出剩下的扑克牌的堆数,并且输出每一堆的牌数. #include<stdio.h> #include<iostre

UVa 127 - &quot;Accordian&quot; Patience POJ 1214 链表题解

UVa和POJ都有这道题. 不同的是UVa要求区分单复数,而POJ不要求. 使用STL做会比较简单,这里纯粹使用指针做了,非常麻烦的指针操作,一不小心就错.调试起来还是非常费力的 本题理解起来也是挺费力的,要搞清楚如何模拟也不容易啊,读题要很仔细. 纯指针的操作挺快的吧.不过POJ 0ms,而UVa就0.2左右了. 三相链表: 1 只要有叠起来的牌,那么就使用一个down指针指向下面的牌就可以了. 2 使用双向链表,可以方便前后遍历. 3 记得有了更新牌之后,又要重新开始检查是否需要更新牌,这是

Uva 127 poj 1214 `Accordian&#39;&#39; 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

ACM学习历程——UVA 127 &quot;Accordian&quot; Patience(栈;模拟)

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

uva ``Accordian&#39;&#39; 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

ACM学习历程——UVA127 &quot;Accordian&quot; Patience(栈, 链表)

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