zoukankan      html  css  js  c++  java
  • UVA-127 "Accordian" Patience (模拟)

    题目大意:一种纸牌游戏,将52张扑克牌排成一列,每次操作可将一张扑克牌移到它的前一张或前面第三张上当牌的点数或花色匹配时。每次都移动最靠左的扑克牌,并且能移动三格就移动三格。求最终扑克牌状态。

    题目分析:利用栈这种数据结构模拟,以为会超时,没想到AC了。

    代码如下:

    # include<iostream>
    # include<cstdio>
    # include<map>
    # include<stack>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    
    struct Poke
    {
        char v,f;
        Poke(char _v,char _f):v(_v),f(_f){}
    };
    char str[2];
    stack<Poke>s[52];
    
    int get(int pos,int k)
    {
        for(int i=pos-1;i>=0;--i){
            if(!s[i].empty()){
                --k;
                if(k==0)  return i;
            }
        }
        return -1;
    }
    
    bool match(const Poke &a,const Poke &b)
    {
        return a.v==b.v||a.f==b.f;
    }
    
    bool toDo(int i,int k)
    {
        Poke a=s[i].top();
        Poke b=s[k].top();
        if(match(a,b)){
            s[k].push(a);
            s[i].pop();
            return true;
        }
        return false;
    }
    
    void solve()
    {
        int i;
        while(1)
        {
            for(i=1;i<52;++i){
                if(s[i].empty())
                    continue;
                int k=get(i,3);
                if(k!=-1&&toDo(i,k))
                    break;
                k=get(i,1);
                if(k!=-1&&toDo(i,k))
                    break;
            }
            if(i>=52)
                break;
        }
    }
    
    int main()
    {
        while(scanf("%s",str))
        {
            if(str[0]=='#')
                break;
            for(int i=0;i<52;++i)
                while(!s[i].empty())
                    s[i].pop();
            s[0].push(Poke(str[0],str[1]));
            for(int i=1;i<52;++i){
                scanf("%s",str);
                s[i].push(Poke(str[0],str[1]));
                Poke u=s[i].top();
            }
            solve();
            int k=0;
            for(int i=0;i<52;++i)
                if(!s[i].empty())
                    ++k;
            if(k==1)
                printf("1 pile remaining: 52
    ");
            else{
                printf("%d piles remaining:",k);
                for(int i=0;i<52;++i)
                    if(!s[i].empty())
                        printf(" %d",s[i].size());
                printf("
    ");
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    npm publish 失败可能的原因记录
    nodejs版实现properties后缀文件解析
    CSS 毛玻璃效果
    echarts markLine 辅助线非直线设置
    sql 数据类型 建表时如何选择数据类型
    用row_nuber 分页的存储过程
    错误描述:未能找到路径“C:/”的一部分
    设置VS2010默认以管理员权限启动
    通过做nopcommerce电商项目对EF的理解(一)
    获取多表联合查询的存储过程。
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4858959.html
Copyright © 2011-2022 走看看