UVA-131
题意:给出 5 张手牌和 5 张备用牌。可以任意丢弃手上的 k 张牌,并从备用牌上方抽 k 张补充。求手上能达到的最大牌面。
解题思路:
首先要搞清楚牌面的大小顺序。
同花顺(AKQJ10,花色相同就行)>四条(四张相同的牌,炸弹 = =) > 福尔豪斯 (aaabb的牌型) > 同花(5张花色相同) > 顺子 (连续,无视花色。A可以当1也可以当14)> 三条 (aaabc的牌型)> 两对 >一对 > 单张大牌。
DFS去枚举扔掉哪些,记住可以取到的最大的牌面。
/*************************************************************************
> File Name: UVA-131.cpp
> Author: Narsh
>
> Created Time: 2016年07月26日 星期二 14时46分37秒
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const string t[9]={"straight-flush","four-of-a-kind",
"full-house","flush","straight","three-of-a-kind",
"two-pairs","one-pair","highest-card"};
int map[6][20],now,ans,num[20],col[6],l;
string hand[10],deck[10];
int value(char ch) {
if (ch == 'T' ) return 10;
if (ch == 'J' ) return 11;
if (ch == 'Q' ) return 12;
if (ch == 'K' ) return 13;
if (ch == 'A' ) return 1;
return ch-'0';
}
int color(char ch) {
if ('S' == ch) return 1;
if (ch == 'H') return 2;
if (ch == 'D') return 3;
if (ch == 'C') return 4;
}
int test() {
int x=0,y=0;
for (int i = 1; i <= 4; i++)
if (map[i][1] && map[i][10] && map[i][11] && map[i][12] && map[i][13]) return 0;
for (int i = 1; i <= 13; i++)
if (num[i] == 4) return 1;
for (int i = 1; i <= 13; i++) {
if (num[i] == 2) x++;
if (num[i] == 3) y++;
}
if (x && y) return 2;
if (!x && y) return 5;
if (x == 2) return 6;
if (x) return 7;
for (int i = 1; i <= 4; i++)
if (col[i] == 5) return 3;
x=0;
for (int i = 1; i <= 13; i++)
if (num[i] != 0) {
x++;
if (x == 5) return 4;
}else x=0;
if (x == 4 && num[1]) return 4;
return 8;
}
void dfs(int i) {
if (i > 5) {
int x=test();
if (x < ans) ans=x;
return ;
}
int x,y;
x=value(hand[i][0]);
y=color(hand[i][1]);
map[y][x] ++;
num[x] ++;
col[y] ++;
dfs(i+1);
num[x]--;
col[y]--;
map[y][x] --;
x=value(deck[l][0]);
y=color(deck[l][1]);
map[y][x]++;
num[x]++;
col[y]++;
l++;
dfs(i+1);
num[x]--;
col[y]--;
map[y][x]--;
l--;
}
int main() {
while (cin>>hand[1]){
printf("Hand: ");
cout<<hand[1]<<" ";
for (int i = 2; i <= 5; i++) {
cin>>hand[i];
cout<<hand[i]<<" ";
}
printf("Deck: ");
for (int i = 1; i <= 5; i++) {
cin>>deck[i];
cout<<deck[i]<<" ";
}
memset(map,0,sizeof(map));
ans=20;
l=1;
dfs(1);
printf("Best hand: ");
cout<<t[ans]<<endl;
}
}