NOIP2015 斗地主 (搜索剪枝)

本文介绍了一种针对简化版斗地主扑克游戏的算法解决方案。通过搜索顺子和其他牌型组合来确定最少出牌次数,实现快速清空手牌的目标。文章详细展示了算法流程及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

牛牛最近迷上了一种叫斗地主的扑克游戏。斗地主是一种使用黑桃、红心、梅花、方片的A到K加上大小王的共54张牌来进行的扑克牌游戏。在斗地主中,牌的大小关系根据牌的数码表示如下:3<4<5<6<7<8<9<10<J<Q<K<A<2<<,而花色并不对牌的大小产生影响。
每一局游戏中,一副手牌由n张牌组成。游戏者每次可以根据规定的牌型进行出牌,首先打光自己的手牌一方取得游戏的胜利。现在,牛牛只想知道,对于自己的若干组手牌,分别最少需要多少次出牌可以将它们打光。请你帮他解决这个问题。
需要注意的是,本题中游戏者每次可以出手的牌型与一般的斗地主相似而略有不同。具体规则如下:

Solution

考虑爆搜,先搜索顺子,在去检查所有的四带二,三带一,三带二,对子,单子即可。

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define For(i , j , k) for (register int i = (j) , _##end_ = (k) ; i <= _##end_ ; ++ i)
#define Fordown(i , j , k) for (register int i = (j) , _##end_ = (k) ; i >= _##end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb push_back
#define INF (0x3f3f3f3f)
#define Mod (1000000007)
using namespace std;
typedef long long LL;

template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; }
template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; }

int _ , __;
char c_;
inline int read()
{
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void file()
{
#ifdef hany01
    freopen("ddz.in" , "r" , stdin);
    freopen("ddz.out" , "w" , stdout);
#endif
}

int n , T , num_ , col_ , cnt[20] , Ans , cnt_[6];

void calc(int cur)
{
    int sum = 0;
    Set(cnt_ , 0);
    For(i , 0 , 14)
        ++ cnt_[cnt[i]];
    while (cnt_[4] && cnt_[2] > 1)
        -- cnt_[4],
        cnt_[2] -= 2,
        ++ sum;
    while (cnt_[4] && cnt_[1] > 1)
        -- cnt_[4],
        cnt_[1] -= 2,
        ++ sum;
    while (cnt_[4] > 1)
        cnt_[4] -= 2,
        ++ sum;
    while (cnt_[3] && cnt_[2])
        -- cnt_[3],
        -- cnt_[2],
        ++ sum;
//    cout << sum << endl;
//    cout << cnt_[3] << ' ' << cnt_[1] << endl;
    while (cnt_[3] && cnt_[1])
        -- cnt_[3],
        -- cnt_[1],
        ++ sum;
//    cout << sum << endl;
//    cout << sum + cnt_[1] + cnt_[2] + cnt_[3] + cnt_[4] + cur << endl;
//    cout << "-----华丽的分割线------" << endl;
    chkmin(Ans , sum + cnt_[1] + cnt_[2] + cnt_[3] + cnt_[4] + cur);
}

void dfs(int cur)
{
    if (cur > Ans)
        return;
    calc(cur);
    int beg_ , end_;
    beg_ = 3;
    end_ = 2;
    For(i , 3 , 14)
    {
        if (cnt[i])
            ++ end_;
        else
        {
            For(ii , beg_ , end_)
                For(jj , ii + 4 , end_)
                {
                    For(j , ii , jj)
                        -- cnt[j];
                    dfs(cur + 1);
                    For(j , ii , jj)
                        ++ cnt[j];
                }
            beg_ = i + 1;
            end_ = i;
        }
    }
    For(ii , beg_ , end_)
        For(jj , ii + 4 , end_)
        {
            For(j , ii , jj)
                -- cnt[j];
            dfs(cur + 1);
            For(j , ii , jj)
                ++ cnt[j];
        }
    beg_ = 3;
    end_ = 2;
    For(i , 3 , 14)
    {
        if (cnt[i] >= 2)
            ++ end_;
        else
        {
            For(ii , beg_ , end_)
                For(jj , ii + 2 , end_)
                {
                    For(j , ii , jj)
                        cnt[j] -= 2;
                    dfs(cur + 1);
                    For(j , ii , jj)
                        cnt[j] += 2;
                }
            beg_ = i + 1;
            end_ = i;
        }
    }
    For(ii , beg_ , end_)
        For(jj , ii + 2 , end_)
        {
            For(j , ii , jj)
                cnt[j] -= 2;
            dfs(cur + 1);
            For(j , ii , jj)
                cnt[j] += 2;
        }
    beg_ = 3;
    end_ = 2;
    For(i , 3 , 14)
    {
        if (cnt[i] >= 3)
            ++ end_;
        else
        {
            For(ii , beg_ , end_)
                For(jj , ii + 1 , end_)
                {
                    For(j , ii , jj)
                        cnt[j] -= 3;
                    dfs(cur + 1);
                    For(j , ii , jj)
                        cnt[j] += 3;
                }
            beg_ = i + 1;
            end_ = i;
        }
    }
    For(ii , beg_ , end_)
        For(jj , ii + 1 , end_)
        {
            For(j , ii , jj)
                cnt[j] -= 3;
            dfs(cur + 1);
            For(j , ii , jj)
                cnt[j] += 3;
        }
}

int main()
{
    file();
    T = read();
    n = read();
    while (T --)
    {
        Set(cnt , 0);
        For(i , 1 , n)
        {
            num_ = read();
            col_ = read();
            if (num_ == 1)
                num_ = 14;
            ++ cnt[num_];
        }
        Ans = INF;
        dfs(0);
        printf("%d\n" , Ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值