`
Jianquan
  • 浏览: 18893 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

UVa 131 The Psychic Poker Player

    博客分类:
  • UVa
阅读更多
题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=107&page=show_problem&problem=67
对于每一个输入样例(10张牌),我们要构造出它所有的32种牌型组合,这是一个比较困难的地方,方法是用递归枚举。每次我们可以从手牌中分别选取0~5张牌,然后剩余的牌从牌堆中补充。从手牌中选牌,就是组合数C(5,0),C(5,1),C(5,2),C(5,3),C(5,4),C(5,5),这道题中,我们不仅要知道组合数是多少,更重要的是把具体的牌型构造出来。
我们可以把构造的牌临时存在temp数组中,然后构造它的每一位,即用5张手牌去填充该位,为了避免重复,假设当前我们用第cur1张手牌(下标从0开始)去填充,那么temp数组的下一位我们只能用第cur1张手牌后面的牌去填充,否则就重复了,因此我们要用一个变量cur1去限制使用手牌去填充的范围(即当前填充范围只能从cur1开始),并且不能超过总牌数-还需构造的牌数。这是一个递归的过程,具体实现请看代码中的structure函数。
当我们把具体的牌型构造出来后,即我们有了5张牌,然后我们就判断它的最好组合牌就可以了。
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cctype>
using namespace std;

struct card
{
	int point;
	char suit;
	bool operator == (const card &b)
	{
		if(point==b.point&&suit==b.suit)
			return 1;
		else return 0;
	}
};

int cur;//sum数组第一维的游标
card hand[5],deck[5],sum[32][5],temp[5];//sum把保存32种牌型

void input(string &tmp,int &point,char &suit)
{
	if(isalpha(tmp[0]))
	{
		switch(tmp[0])
		{
			case'A':point=1;break;
			case'T':point=10;break;
			case'J':point=11;break;
			case'Q':point=12;break;
			case'K':point=13;break;
		}
	}
	else
		point=tmp[0]-'0';
	suit=tmp[1];
}
void structure(int X,int cur1,int cur2,card *hand,card *temp)
{//还需选择X张牌,目前正在从hand数组的第cur1位开始构造temp数组的第cur2位
	if(X==0) 
	{
		for(int i=cur2;i<5;i++)//牌堆中选出牌补充
			temp[i]=deck[i-cur2];
		memcpy(sum[cur++],temp,sizeof(card)*5);
		return;
	}
	for(int i=cur1;i<=5-X;i++)
	{
		temp[cur2]=hand[i];
		structure(X-1,i+1,cur2+1,hand,temp);
	}
}
int cmp(void const *a,void const *b)
{
	return ((card *)a)->point-((card *)b)->point;
}
int solve(card *temp)//对于给定的5张牌,判断它的最好组合牌
{
	int i,flush=1,straight=0;
	qsort(temp,5,sizeof(card),cmp);
	if(temp[4].point-temp[3].point==1&&temp[3].point-temp[2].point==1&&temp[2].point-temp[1].point==1&&temp[1].point-temp[0].point==1||temp[0].point==1&&temp[1].point==10&&temp[2].point==11&&temp[3].point==12&&temp[4].point==13)
		straight=1;
	for(i=1;i<5;i++)
		if(temp[i].suit!=temp[0].suit)
		{
			flush=0;
			break;
		}
	if(flush&&straight) return 9;
	int cnt[14]={0},four=0,three=0,two=0;
	for(i=0;i<5;i++)
		cnt[temp[i].point]++;
	for(i=1;i<=13;i++) if(cnt[i]>1)
	{
		if(cnt[i]==2) two++;
		else if(cnt[i]==3) three++;
		else four++;	
	}
	if(four) return 8;
	if(three&&two) return 7;
	if(flush) return 6;
	if(straight) return 5;
	if(three) return 4;
	if(two==2) return 3;
	if(two) return 2;
	return 1;
}
int main()
{
	//freopen("in.txt","r",stdin);
	int i;
	string tmp;
	while(cin>>tmp)
	{
		cout<<"Hand: ";
		cout<<tmp<<' ';
		input(tmp,hand[0].point,hand[0].suit);
		for(i=1;i<5;i++)
		{
			cin>>tmp;
			cout<<tmp<<' ';
			input(tmp,hand[i].point,hand[i].suit);
		}
		cout<<"Deck: ";
		for(i=0;i<5;i++)
		{
			cin>>tmp;
			cout<<tmp<<' ';
			input(tmp,deck[i].point,deck[i].suit);
		}
		cur=0;
		//构造5张牌的全部情况,存放在sum中
		for(i=0;i<=5;i++)//手牌中选i张
			structure(i,0,0,hand,temp);
		int best=1;
		for(i=0;i<32;i++)
		{
			int t=solve(sum[i]);
			if(t>best) best=t;
			if(best==9) break;
		}
		cout<<"Best hand: ";
		switch(best)
		{
			case 1:cout<<"highest-card"<<endl;break;
			case 2:cout<<"one-pair"<<endl;break;
			case 3:cout<<"two-pairs"<<endl;break;
			case 4:cout<<"three-of-a-kind"<<endl;break;
			case 5:cout<<"straight"<<endl;break;
			case 6:cout<<"flush"<<endl;break;
			case 7:cout<<"full-house"<<endl;break;
			case 8:cout<<"four-of-a-kind"<<endl;break;
			case 9:cout<<"straight-flush"<<endl;break;
		}
	}
	return 0;
}
分享到:
评论

相关推荐

    The-Psychic-Poker-Player:解决方案

    通灵扑克玩家解决方法: :

    cpp-Psychic基于psyc的ARM板的深度学习库

    基于PsyC的一些最常见的人工神经网络模型的C实现。

    Eye of the Psychic-Paranormal Investigations-crx插件

    语言:English 进入令人兴奋的令人兴奋的全球调查世界和精神审查 随着互联网的增长,人们提出问题的人数也是如此。我们不再绑在等待书籍,以便获得我们正在寻找的答案。它已成为一个伟大的教育工具和一种启发别人的...

    psychic-ui:OpenGL UI库

    psychic-ui:OpenGL UI库

    Python库 | psychic_disco-0.10.0.tar.gz

    python库。 资源全名:psychic_disco-0.10.0.tar.gz

    Psychics:适用于Paper的Minecraft Psychic插件

    心理 소개 发布。 용법개발개발 编译 ./gradlew构建 插件= ./psychics-common/build/libs/Psychics.jar 能力= ./psychics-abilities/build/libs/GroupName.AbilityName.jar 依赖插件 ... 후PaperMC 1.16.3구동하세요...

    psychic-disco

    测试 01.访问日志分析 运行环境: Linux(Ubuntu优先) 先决条件: 卷曲 q 操作说明: 从ftp://ita.ee.lbl.gov/traces/NASA_access_log_Aug95.gz下载日志文件 解压下载的文件并将其放入question1文件夹 ...

    psychic-sniffle:https

    psychic-sniffle:https

    Master Psychic Online-crx插件

    通过Master Psychic Rachel在线获得免费或付费的心理阅读 与Internet排名最高的在线心理学家之一,Master Psychic Rachel一起获得免费或付费的心理学读物。 * MasterPsychicOnline.com是您免费心理阅读的第一来源。 ...

    Psychic Messenger 3 Free Psychic Answers-crx插件

    Psychic Messenger 3免费通灵问答 获得3个免费的心理答案,当您可以获得其他免费的心理答案时,会收到新优惠的通知。 如果您有需要心理答案的问题,或者想知道将来会发生什么,这是给您的! 关系答案爱情生活答案...

    psychic_game

    psychic_game

    Psychic-Game:猜猜计算机在想什么!

    通灵游戏 猜猜计算机在想什么!

    psychic

    精神sharkey告诉我16岁的时候,如果我多呼吸,直到我头昏眼花,我就不再通灵了,这与他的生意有关。 因为他的生意全是通灵的,这就是他的心灵感应。 我想知道我想告诉中央情报局...帮助我。 这种鲨鱼是疯狂的。...

    Psychic Awakening Software-crx插件

    此Psychic Awakening软件使用模式识别(您的大脑功能)在100%随机静态范围内找到潜意识图像(在右侧启用Adobe Flash)。 有了开放思想,您也可以在此计算机屏幕和网站上看到“心理思想图像”。 显示“静态”后,您...

    掌握心理在线「Master Psychic Online」-crx插件

    与Internet排名最高的在线心理学家之一,Master Psychic Rachel一起获得免费或付费的心理学读物。 * MasterPsychicOnline.com是您免费心理阅读的第一来源。 *首次客户可以每月支付1倍费用,并获得免费的答案。 * Vip...

    Psychic-Game:JavaScript分配

    答:这是一个JavaScript项目,其中包含一个名为Psychic Game的游戏。 它允许玩家尝试猜测通灵者在想什么字母。 问:为什么这个项目有用? 答:这个项目很有用,因为它可以帮助人们获得乐趣。 问:如何开始这个...

    matlab距离矩阵代码-PSYCHIC:使用Hi-C数据查找推定的增强子的代码

    matlab距离矩阵代码精神 使用Hi-C数据查找推定的增强子的代码 用法 python htad-​​chain.py 要运行该示例,请使用repo目录中的python htad-chain.py examples/himr90.chr20.conf 。...Hi-C文件的res分辨率,以基为...

    mongod-client:psychic-octo MonGOD 应用程序的客户端

    mongod 客户端 psychic-octo MonGOD 应用程序的客户端

Global site tag (gtag.js) - Google Analytics