UVA 127 "Accordian" Patience

题意:

  按从左至右的顺序发牌,并摆成一行,发牌不要相互重叠。游戏中一旦出现任何一张牌与它左边的第一张或第三张“匹配”,即花色或点数相同,则须立即将其移动到那张牌上面。如果牌被移动后又出现了上述情况,则需再次向左移动。每叠牌只能移动最上面的一张。如果一叠牌被移空,应该立即将右边各叠整体向左移动,补上这个空隙。依次将整副牌都发完,并不断的向左合并。如果全部移动结束后整副牌都放成了一叠,则游戏胜利。

分析:

  用sum表示有多少个牌挪动了,最后输出52-sum。判断是否能放到左边第三张的时候需要注意再前面是否有空位置,空位置不算数。

代码:

  

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;char card[54][54][3];int top[54];int sum;int match( char* a, char *b ){    return (a[0] == b[0] || a[1] == b[1]);}int deal(int now,int s){    int cont=0,temp=now;    while(temp>=0&&cont<s)    {        if(top[--temp]>=0)            cont++;
    }//cout<<cont<<endl;    if(temp>=0&&match(card[now][top[now]],card[temp][top[temp]]))    {        top[temp]++;        card[temp][top[temp]][0]=card[now][top[now]][0];        card[temp][top[temp]][1]=card[now][top[now]][1];        if (--top[now]<0)            sum ++;        return temp;    }    else        return -1;}int main(){    while(scanf("%s",card[0][0])&&card[0][0][0]!=‘#‘)    {        int i,j;        for(i=1;i<52;i++)            scanf("%s",card[i][0]);        for(i=0;i<52;i++)            top[i]=0;        sum=0;        for(int now=1;now<52;)        {
            while(top[now]<0)                now++;            //cout<<now<<endl;            int save=deal(now,3);            //cout<<save<<endl;            if(save>= 0 )                now=save;            else            {                save=deal(now,1);                //cout<<1<<" "<<save<<endl;                if(save >= 0)                    now=save;                else                    now++;            }            //cout<<endl;
        }        printf("%d pile",52-sum);        if (51>sum)            printf("s");        printf(" remaining:");        for (i=0;i<52;++i)            if (top[i]>=0)                printf(" %d",top[i]+1);        printf("\n");    }}
时间: 2024-12-29 11:02:22

UVA 127 "Accordian" Patience的相关文章

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 记得有了更新牌之后,又要重新开始检查是否需要更新牌,这是

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 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

127 - &quot;Accordian&quot; Patience

很简单呢的一道题,定义了一个结构体数组,用vector来做为一个牌堆.将尾部作为牌堆的顶部. 要注意,当最后只剩一堆牌的时候,答案中的pile不加s #include<bits/stdc++.h> using namespace std; const int maxn = 60; int b[60]; struct pile { vector<string> a; }; int main() { ios::sync_with_stdio(false); string buf; wh

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

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

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

&quot;Accordian&quot; 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 th

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