题目大意:就是德州扑克的题目。。。。
现在手上有5张牌,另外桌上还有五张牌,只能一次顶端取,现在问这个人通过舍弃手上的某些牌来获得牌的价值最大。。。
赤裸裸的暴力搜索 + 子集枚举
若加上一个最优化剪枝时间可以达到0.001秒。。。或许是我想多了。。
剪枝:就是判断当前牌的最大价值时,与已经得到的最大价值比较max,只需要判断当前的牌的价值能否大于max,如果没有大于max的下面的价值就不需要判断了。
//
// main.cpp
// uva 131 - The Psychic Poker Player
//
// Created by XD on 15/8/8.
// Copyright (c) 2015年 XD. All rights reserved.
//
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;
int best ;
char b[10][20]
{{""} ,{"highest-card"} ,{"one-pair"} ,{"two-pairs"} ,{"three-of-a-kind"} ,{"straight"} ,{"flush"} ,{"full-house"} ,{"four-of-a-kind"} ,{"straight-flush"} ,
} ;
struct card
{
int m , t ;
bool operator < (const card &n ) const{
if(m == n.m )
{
return t < n.t ;
}
return m < n.m ;
} ;
};
card hand[6] ;
card deck[6] ;
card thand[6] ;
int bestKind()
{
if (best >= 9 ) {
return 0 ;
}
int flag = 0 ;
sort(thand, thand + 5 ) ;
//Best hand: straight-flush
for (int i = 1 ; i <5; i++) {
if (thand[i].m != thand[i-1].m+1 || thand[i].t != thand[i-1].t) {
flag = 1 ;
break ;
}
}
if (!flag) {
return 9 ;
}
//Best hand: four-of-a-kind
if (best >= 8) {
return 0 ;
}
int num ;
for (int i= 1; i < 5; i++) {
if (thand[i].m == thand[i-1].m) {
num = 2 ;
i++ ;
while (i < 5 && thand[i].m == thand[i-1].m) {
num++ ;
i++ ;
}
if (num == 4 ) {
return 8 ;
}
flag= 0 ;
break ;
}
}
//Best hand: full-house
if (best >= 7) {
return 0 ;
}
if ((thand[0].m == thand[1].m&& thand[2].m== thand[3].m&& thand[3].m ==thand[4].m)|| (thand[0].m == thand[1].m&& thand[1].m== thand[2].m && thand[3].m ==thand[4].m)) {
return 7 ;
}
//Best hand: flush
if (best >= 6) {
return 0 ;
}
flag =0 ;
for (int i = 1 ; i <5; i++) {
if (thand[i].t != thand[i-1].t) {
flag = 1 ;
break ;
}
}
if (flag == 0 ) {
return 6 ;
}
//Best hand: straight
if (best >= 5) {
return 0 ;
}
flag = 0 ;
for (int i = 1 ; i <4; i++) {
if (thand[i].m != thand[i-1].m+1) {
flag = 1 ;
break ;
}
}
if (thand[4].m==14 && flag==0) {
if (thand[0].m==2||thand[3].m==13) {
flag= 0 ;
}
else{
flag=1 ;
}
}
if (!flag) {
return 5 ;
}
if (best >= 4) {
return 0 ;
}
//Best hand: three-of-a-kind
flag=0 ;
for (int i = 0; i <= 2; i++) {
if (thand[i].m == thand[i+1].m&&thand[i+1].m ==thand[i+2].m) {
return 4 ;
}
}
//Best hand: two-pairs
//Best hand: one-pair
//Best hand: highest-card
if (best >= 3) {
return 0 ;
}
num = 0 ;
for (int i = 1; i < 5; i++) {
if(thand[i].m == thand[i-1].m)
{
num++ ;
}
}
return num+1 ;
}
int changem(char ch)
{
if (ch >='2'&&ch<='9') {
return ch-'0' ;
}
else if (ch == 'A')
{
return 14 ;
}
else if(ch=='T'){
return 10;
}
else if(ch == 'J'){
return 11 ;
}
else if(ch=='Q')
{
return 12 ;
}
else if (ch=='K')
{
return 13 ;
}
return 0 ;
}
int Getchar(int t)
{
switch (t) {
case 14:
return 'A';
break;
case 10:
return 'T' ;
case 11:
return 'J' ;
case 12:
return 'Q' ;
case 13 :
return 'K' ;
default:
return t + '0' ;
break;
}
}
int main() {
while(true)
{
char s[3] ;
for (int i = 0; i < 5 ; i++) {
if(scanf(" %s" ,s )!=1) return 0 ; ;
hand[i].m = changem(s[0]) ;
hand[i].t = s[1] ;
}
for (int i =0; i < 5 ; i++) {
scanf(" %s" , s) ;
deck[i].m = changem(s[0]) ;
deck[i].t = s[1] ;
}
int end = 1<< 5 ;
int g = sizeof(hand) ;
best = 0 ;
for (int i = 0; i < end; i++) {
memcpy(thand, hand, g) ;
int t = 0 ;
for (int j = 0 ; j < 5; j++) {
if(i&(1<<j))
{
thand[j] = deck[t++] ;
}
}
int tb = bestKind() ;
best = tb > best ? tb :best ;
if (best== 9 ) {
break ;
}
}
printf("Hand:") ;
for (int i = 0; i < 5; i++) {
printf(" %c%c" , Getchar(hand[i].m) ,hand[i].t) ;
}
printf(" Deck:") ;
for (int i = 0; i < 5; i++) {
printf(" %c%c" , Getchar(deck[i].m) ,deck[i].t) ;
}
printf(" Best hand: %s\n",b[best]) ;
}
return 0;
}