您的位置:首页 > 其它

BZOJ 4325: NOIP2015 斗地主 爆搜

2017-08-28 09:47 453 查看

4325: NOIP2015 斗地主

Time Limit: 30 Sec  Memory Limit: 1024 MB
Submit: 867  Solved: 588

[Submit][Status][Discuss]

Description

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



Input

第一行包含用空格隔开的2个正整数T,N,表示手牌的组数以及每组手牌的张数。

接下来T组数据,每组数据N行,每行一个非负整数对Ai,Bi,表示一张牌,其中Ai表示牌的数码,Bi表示牌的花色,中间用空格隔开。特别的,我们用1来表示数码A,11表示数码J,12表示数码Q,13表示数码K;黑桃、红心、梅花、方片分别用1-4来表示;小王的表示方法为01,大王的表示方法为02。

Output

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

Sample Input

1 8

7 4

8 4

9 1

10 4

11 1

5 1

1 4

1 1

Sample Output

3

HINT

 共有1组手牌,包含8张牌:方片7,方片8,黑桃9,方片10,黑桃J,黑桃5,方片A以及黑桃A。可以通过打单顺子(方片7,方片8,黑桃9,方片10,黑桃J),单张牌(黑桃5)以及对子牌(黑桃A以及方片A)在3次内打光。

T<=10
N<=23

这可真是一道爆搜好题啊。。。

想状压记忆化搜索,结果枚举子集有点复杂度爆炸,滚回去抄搜索

感觉挺6的,不多说了,看代码吧

跑的还挺快

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

int hand[20],cnt[5],ans,n;

void dfs(int step)
{
if(step>ans)return ;
register int i,j,rest=0;
memset(cnt,0,sizeof(cnt));
for(i=0;i<15;++i)cnt[hand[i]]++;
while(cnt[4])
{
cnt[4]--;rest++;
if(cnt[2]>=2)cnt[2]-=2;
else if(cnt[1]>=2)cnt[1]-=2;
}
while(cnt[3])
{
cnt[3]--;rest++;
if(cnt[2])cnt[2]--;
else if(cnt[1])cnt[1]--;
}
if(hand[0]&&hand[1]&&cnt[1]>=2)rest--;
rest+=cnt[1]+cnt[2];
ans=min(rest+step,ans);
for(i=3;i<15;++i)
{
for(j=i;hand[j]&&j<15;++j)
{hand[j]--;if(j-i+1>=5)dfs(step+1);}
while(j>i)hand[--j]++;
}
for(i=3;i<15;++i)
{
for(j=i;hand[j]>=2&&j<15;++j)
{hand[j]-=2;if(j-i+1>=3)dfs(step+1);}
while(j>i)hand[--j]+=2;
}
for(i=3;i<15;++i)
{
for(j=i;hand[j]>=3&&j<15;++j)
{hand[j]-=3;if(j-i+1>=2)dfs(step+1);}
while(j>i)hand[--j]+=3;
}
}

int main()
{
int T=read();n=read();
register int i,x,y;
while(T--)
{
memset(hand,0,sizeof(hand));
for(i=1;i<=n;++i)
{
x=read();y=read();
if(!x)hand[y-1]++;
else if(x==1)hand[14]++;
else hand[x]++;
}
ans=n;
dfs(0);print(ans);puts("");
}
return 0;
}
/*
1 8
7 4
8 4
9 1
10 4
11 1
5 1
1 4
1 1

3
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: