UVa 131 - The Psychic Poker Player

题目:手里有五张牌,桌上有一堆牌(五张),你可以弃掉手中的k张牌,然后从牌堆中取最上面的k个。

比较规则如下:(按优先级排序)

1.straight-flush:同花顺,牌面为T(10) - A,这里不论花色是否相同;

2.four-of-a-kind:四条,牌面有4个相同的值;

3.full-house:船牌,牌面有3个相同值,剩下2个也相同值;

4.flush:同花,五张牌的花色相同,不是同花顺;

5.straight:顺子,五张牌的值连续,A可以作为1也可以作为14;

6.three-of-a-kind:三条,牌面有3个相同的值;

7.two-pairs:两对,牌面有2个对子;

8.one-pair:一对,牌面有一个对子,即2个同值;

9.highest-card:大牌,没有以上牌型。

分析:搜索、枚举。枚举换牌数从0-5的每种情况,判断,取出优先值最高的即可。

说明:读题是重点。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

#define min(x,y) ((x)<(y)?(x):(y))

using namespace std;

char temp[5][3];
char card[10][3];
int  maps[5][13];

char output[11][20] = {
	"","straight-flush","four-of-a-kind",
	"full-house","flush","straight","three-of-a-kind",
	"two-pairs","one-pair","highest-card",""};

int  value( char ch )
{
	if ( ch == 'T' ) return 9;
	if ( ch == 'J' ) return 10;
	if ( ch == 'Q' ) return 11;
	if ( ch == 'K' ) return 12;
	if ( ch == 'A' ) return 0;
	return ch - '1';
}

int  color( char ch )
{
	if ( ch == 'S' ) return 0;
	if ( ch == 'H' ) return 1;
	if ( ch == 'D' ) return 2;
	if ( ch == 'C' ) return 3;
}

int  tests()
{
	//royal-flush | straight-flush
	for ( int i = 0 ; i < 5 ; ++ i )
		if ( maps[i][0]&maps[i][9]&maps[i][10]&maps[i][11]&maps[i][12] )
			return 1;
	//four-of-a-kind
	for ( int i = 0 ; i < 13 ; ++ i )
		if ( maps[4][i] == 4 ) return 2;
	//full-house
	int three = 0,two = 0;
	for ( int i = 0 ; i < 13 ; ++ i ) {
		if ( maps[4][i] == 2 ) two ++;
		if ( maps[4][i] == 3 ) three ++;
	}
	if ( two && three ) return 3;
	//flush
	for ( int i = 0 ; i < 4 ; ++ i ) {
		int count = 0;
		for ( int j = 0 ; j < 13 ; ++ j )
			count += maps[i][j];
		if ( count >= 5 ) return 4;
	}
	//straight
	for ( int i = 0 ; i < 10 ; ++ i )
		if ( maps[4][i]&maps[4][i+1]&maps[4][i+2]&maps[4][i+3]&maps[4][(i+4)%13] )
			return 5;
	//three-of-a-kind
	if ( three ) return 6;
	//two-pairs
	if ( two > 1 ) return 7;
	//one-pair
	if ( two ) return 8;
	return 9;
}

void change()
{
	for ( int i = 0 ; i < 5 ; ++ i )
		strcpy(temp[i],card[i+5]);
	memset( maps, 0, sizeof(maps) );
	for ( int i = 0 ; i < 5 ; ++ i ) {
		maps[color(temp[i][1])][value(temp[i][0])] = 1;
		maps[4][value(temp[i][0])] ++;
	}
	int Min = tests();

	for ( int k = 1 ; k <= 5 ; ++ k ) {
    	int xx,yy,comb = (1<<k)-1;
        while ( comb < 32 ) {
            // 计算当前状态对应的集合
            int j = 0,count = 0,move = 5;
            do {
                if ( (1<<j)&comb )
					strcpy(temp[count ++],card[j]);
                j ++;
            }while ( j < 5 );

            while ( count < 5 )
				strcpy(temp[count ++],card[move ++]);

			memset( maps, 0, sizeof(maps) );
			for ( int i = 0 ; i < 5 ; ++ i ) {
				maps[color(temp[i][1])][value(temp[i][0])] = 1;
				maps[4][value(temp[i][0])] ++;
			}

			Min = min(Min,tests());
            // 位运算计算下一组合
            xx = comb&-comb,yy = comb+xx;
            comb = ((comb&~yy)/xx>>1)|yy;
        }
    }
    printf("%s\n",output[Min]);
}

int main()
{
	while ( ~scanf("%s",card[0]) ) {
		for ( int i = 1 ; i < 10 ; ++ i )
			scanf("%s",card[i]);

		printf("Hand: ");
		for ( int i = 0 ; i < 5 ; ++ i )
			printf("%s ",card[i]);
		printf("Deck: ");
		for ( int i = 5 ; i < 10 ; ++ i )
			printf("%s ",card[i]);
		printf("Best hand: ");
		change();
	}
	return 0;
}

UVa 131 - The Psychic Poker Player,布布扣,bubuko.com

时间: 2024-08-01 02:50:29

UVa 131 - The Psychic Poker Player的相关文章

uva 131 The Psychic Poker Player (暴力枚举)

uva 131 The Psychic Poker Player  The Psychic Poker Player  In 5-card draw poker, a player is dealt a hand of five cards (which may be looked at). The player may then discard between zero and five of his or her cards and have them replaced by the sam

UVA131 HDU1629 The Psychic Poker Player【暴力】

The Psychic Poker Player Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 255 Accepted Submission(s): 49 Problem Description In 5-card draw poker, a player is dealt a hand of five cards (which may

UVa 131 有超能力的纸牌玩家

题意:题目描述太简单了,简直需要猜题目.看别人的题解也知道题目意思.大致意思是,手里五张牌,可以弃0~5张,然后从牌堆的5张中拿最上面的同等数量纸牌,使得value最大.value的评价真的是要猜...依次为: straight-flush 同花顺four-of-a-kind  炸弹full-house   满堂红 三张同点牌加上一对 flush 同花straight 顺子(注意A即可接2也可以接K)three-of-a-kind   三张相同的牌 two-pairs 两对对子 one-pair

uva 131

题目大意:手上有5张牌, 堆上有5张牌, 现在要求舍弃手上的n(0<=n<=5)牌, 从堆的面上拿n张牌, 假设了你有特异功能, 可以知道堆上的5张牌从上到下是是什么, 要求最后得到的牌要面值最大, 一张牌由数字与花色组成. straight-flush 同花顺:一手同花的五张牌 + 五张牌点数连续的顺子 four-of-a-kind  四张相同 的牌 full-house     满堂红:三张同点牌加上一对(此时存在2张不同点的牌) flush 一手同花的五张牌 straight  顺子:五

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

[IOS] Storyboard全解析-第一部分

(Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果,我贴上本教程所完成的Storyboard的截图: 现在,你就可以清楚的看到这个应用究竟是干些什么的,也可以清楚的看到其中的各种关系,这就是Storyboard的强大之处了.如果你要制作一个页面很多很复杂的App,Storyboard可以帮助你解决写很多重复的跳转方法的麻烦,节省很多时间,以便你能够完全的专注于核心功能的实现上. 开始 首先启动Xcode,新建一个工程,我们在

Storyboard 全解析

XCode 4.3.2 新功能 - Storyboard 最近开始比较有空在玩 XCode 4.3.2,赫然发现它多了个 Storyboard 的东东. Storyboard 这个东西一般来说是在做创意发想的时候,用来将自己的想的一些故事情节画成像是连环漫画一样,想不到 Apple 把它用在这里,真是佩服... 好吧,不废话,先来说说这个 Storyboard 带来什么改变? 在这个版本前,我们在设计画面的时候都是用 interface builder 产生一个 xib 档,然后在 code 要

lrj紫书第五章

UVA-1592 1 // UVa1592 Database 2 // Rujia Liu 3 // 本程序只是为了演示STL各种用法,效率较低.实践中一般用C字符串和哈希表来实现. 4 5 #include<iostream> 6 #include<cstdio> 7 #include<vector> 8 #include<string> 9 #include<map> 10 #include<sstream> 11 using n

IOS开发之Storyboard应用

制作一个Tab类型的应用 制作一个表格视图 原型表格单元 设计自定义的原型单元格 为原型单元格设置子类 故事版(Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明的说明Storyboard的效果,我贴上本教程所完成的Storyboard的截图:   现在,你就可以清楚的看到这个应用究竟是干些什么的,也可以清楚的看到其中的各种关系,这就是Storyboard的强大之处了.如果你要制作一个页面很 多很复杂的App,Storyboard可以帮助你解决写很多重复的跳