zoukankan      html  css  js  c++  java
  • NOIP 2015 斗地主

    NOIP 2015 斗地主

    洛谷传送门

    JDOJ传送门

    Description

    牛牛最近迷上了一种叫斗地主的扑克游戏。斗地主是一种使用黑桃、红心、梅花、方片的A到K加上大小王的共54张牌来进行的扑克牌游戏。在斗地主中,牌的大小关系根据牌的数码表示如下:3<4<5<6<7<8<9<10<J<Q<K<A<2<小王<大王,而花色并不对牌的大小产生影响。每一局游戏中,一副手牌由n张牌组成。游戏者每次可以根据规定的牌型进行出牌,首先打光自己的手牌一方取得游戏的胜利。

    现在,牛牛只想知道,对于自己的若干组手牌,分别最少需要多少次出牌可以将它们打光。请你帮他解决这个问题。

    需要注意的是,本题中游戏者每次可以出手的牌型与一般的斗地主相似而略有不同。具体规则如下:

    img

    Input

    第一行包含用空格隔开的2个正整数 T , n ,表示手牌的组数以及每组手牌的张数。
    接下来 T 组数据,每组数据 n 行,每行一个非负整数对 ai , bi ,表示一张牌,其中 ai 表示牌的数码,bi 表示牌的花色,中间用空格隔开。特别的,我们用 1 来表示数码A,11表示数码J,12表示数码Q,13表示数码K;黑桃、红心、梅花、方片分别用1-4来表示;小王的表示方法为0 1,大王的表示方法为0 2。

    Output

    共 T 行,每行一个整数,表示打光第 i 组手牌的最少次数。

    Sample Input

    1 8 7 4 8 4 9 1 10 4 11 1 5 1 1 4 1 1

    Sample Output

    3

    HINT

    【数据规模与约定】
    对于不同的测试点,我们约定手牌组数 T 与张数 n 的规模如下:
    数据保证:所有的手牌都是随机生成的。

    测试点编号 T n 测试点编号 T n
    1 100 2 11 100 14
    2 100 2 12 100 15
    3 100 3 13 10 16
    4 100 3 14 10 17
    5 100 4 15 10 18
    6 100 4 16 10 19
    7 100 10 17 10 20
    8 100 11 18 10 21
    9 100 12 19 10 22
    10 100 13 20 10 23

    最优解声明:


    题解:

    %%%枫哥

    搜索+大模拟。

    考场上反正我是不敢开正解,这题太细了(bushi

    所以先打了个30p垫底。

    30pts:

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int n,tot,opt;
    int cnt[16];
    bool flag;
    int main()
    {
        int t;
        scanf("%d%d",&t,&n);
        while(t--)
        {
            memset(cnt,0,sizeof(cnt));
            flag=0,tot=0;
            for(int i=1,x;i<=n;i++)
            {
                scanf("%d%d",&opt,&x);
                cnt[opt]++;
            }
            if(n==2)
            {
                for(int i=0;i<=13;i++)
                    if(cnt[i]==2)
                    {
                        puts("1");
                        flag=1;
                        break;
                    }
                if(flag)
                    continue;
                puts("2");
            }
            if(n>=3)
            {
                for(int i=0;i<=13;i++)
                {
                    if(cnt[i]>=3)
                    {
                        puts("1");
                        flag=1;
                        break;
                    }
                    else if(cnt[i])
                        tot++;
                }
                if(flag)
                    continue;
                printf("%d
    ",tot);
            }
        }
        return 0;
    }
    

    好了,讲正解。

    剪枝用最优化剪枝,如果当前步数大于已知最小步数,直接停。

    剩下的,依题意模拟即可。(别打我)

    代码有注释。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int INF=998244353;
    int t,n,ans;
    int cnt[20];
    bool prune(int x)
    {
        if(x>=ans)
            return 1;
        return 0;
    }
    void shengxiade(int x)
    {
        int now=x;
        for(int i=2;i<=15;i++)
            if(cnt[i])
                now++;
        ans=min(ans,now);
    }
    void dfs(int x)
    {
        if(prune(x))
            return;//剪枝
        //以下是顺子部分
        int tot=0;
        for(int i=3;i<=14;i++)
            if(cnt[i]<1)
                tot=0;
            else
            {
                tot++;
                if(tot>=5)
                {
                    for(int j=i-tot+1;j<=i;j++)
                        cnt[j]--;
                    dfs(x+1);
                    for(int j=i-tot+1;j<=i;j++)
                        cnt[j]++;
                }
            }//单
        tot=0;
        for(int i=3;i<=14;i++)
            if(cnt[i]<2)
                tot=0;
            else
            {
                tot++;
                if(tot>=3)
                {
                    for(int j=i-tot+1;j<=i;j++)
                        cnt[j]-=2;
                    dfs(x+1);
                    for(int j=i-tot+1;j<=i;j++)
                        cnt[j]+=2;
                }
            }//双
        tot=0;
        for(int i=3;i<=14;i++)
            if(cnt[i]<3)
                tot=0;
            else
            {
                tot++;
                if(tot>=2)
                {
                    for(int j=i-tot+1;j<=i;j++)
                        cnt[j]-=3;
                    dfs(x+1);
                    for(int j=i-tot+1;j<=i;j++)
                        cnt[j]+=3;
                }
            }//三
        //以下是带牌部分
        for(int i=2;i<=14;i++)
        {
            if(cnt[i]==3)
            {
                for(int j=2;j<=14;j++)
                {
                    if(j==i||cnt[j]<2)
                        continue;
                    cnt[i]-=3;
                    cnt[j]-=2;
                    dfs(x+1);
                    cnt[i]+=3;
                    cnt[j]+=2;
                }//三带二
                for(int j=2;j<=15;j++)
                {
                    if(j==i||cnt[j]<1)
                        continue;
                    cnt[i]-=3;
                    cnt[j]--;
                    dfs(x+1);
                    cnt[i]+=3;
                    cnt[j]++;
                }//三带一
            }
            if(cnt[i]==4)
            {
                for(int j=2;j<=14;j++)
                {
                    if(j==i||cnt[j]<2)
                        continue;
                    cnt[i]-=3;
                    cnt[j]-=2;
                    dfs(x+1);
                    cnt[i]+=3;
                    cnt[j]+=2;
                }//三带二
                for(int j=2;j<=15;j++)
                {
                    if(j==i||cnt[j]<1)
                        continue;
                    cnt[i]-=3;
                    cnt[j]--;
                    dfs(x+1);
                    cnt[i]+=3;
                    cnt[j]++;
                }//三带一
                for(int j=2;j<=14;j++)
                {
                    if(j==i||cnt[j]<2)
                        continue;
                    for(int k=2;k<=14;k++)
                    {
                        if(k==i||k==j||cnt[k]<2)
                            continue;
                        cnt[i]-=4;
                        cnt[j]-=2;
                        cnt[k]-=2;
                        dfs(x+1);
                        cnt[i]+=4;
                        cnt[j]+=2;
                        cnt[k]+=2;
                    }
                }//四带二
                for(int j=2;j<=15;j++)
                {
                    if(j==i||cnt[j]<1)
                        continue;
                    for(int k=2;k<=15;k++)
                    {
                        if(k==i||k==j||cnt[k]<1)
                            continue;
                        cnt[i]-=4;
                        cnt[j]--;
                        cnt[k]--;
                        dfs(x+1);
                        cnt[i]+=4;
                        cnt[j]++;
                        cnt[k]++;
                    }
                }//四带一
            }
        }
        shengxiade(x);
    }
    int main()
    {
        scanf("%d%d",&t,&n);
        while(t--)
        {
            memset(cnt,0,sizeof(cnt));
            ans=INF;
            for(int i=1,x,y;i<=n;i++)
            {
                scanf("%d%d",&x,&y);
                if(x==0)
                    cnt[15]++;
                else if(x==1)
                    cnt[14]++;
                else
                    cnt[x]++;
            }
            dfs(0);
            printf("%d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    hdu2588-GCD-(欧拉函数+分解因子)
    欧拉定理及其扩展定理公式
    hdu2973-YAPTCHA-(欧拉筛+威尔逊定理+前缀和)
    hdu5391-Zball in Tina Town-威尔逊定理(假证明)
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13830034.html
Copyright © 2011-2022 走看看