题目大意:德州扑克。手上五张牌(无序),桌上五张牌(有序,从上到下抽取,能看见牌面),每次能丢弃手中的一张牌换取桌上最上面的一张,保持手中牌数五张。问能组成最大的牌型。
解题思路:
德州扑克的规则(大到小) | |
---|---|
同花顺(straight-flush) | 同一花色的五张连牌 |
炸弹(four-of-a-kind) | 四张点数相同 |
葫芦(full-house) | 三条+一对 |
同花(flush) | 五张花色相同 |
顺子(straight) | 五张连牌 |
三条(three-of-a-kind) | 三张点数相同 |
两对(two-pairs) | 两对点数相同 |
一对(one-pair) | 一对点数相同 |
高牌(highest-card) | 散牌 |
然后就是dfs,当牌数 >5 就可以成牌,serch() 一下牌型判断是否比之前大。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
string a[9]={"straight-flush","four-of-a-kind","full-house","flush","straight","three-of-a-kind","two-pairs","one-pair","highest-card"};
string hand[10], deck[10];
int num[20], col[10];
int ans, n;
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 (ch == 'S') return 1;
if (ch == 'H') return 2;
if (ch == 'D') return 3;
if (ch == 'C') return 4;
}
int search() {
int st = 0, two = 0, three = 0;
//straight flush
for (int i = 1; i <= 4; i++)
if (col[i] == 5) {
for (int j = 1; j <= 13; j++)
if (num[j] != 0) {
st++;
if (st == 5) return 0;
if (st == 4 && num[1]) return 0;
}
else st = 0;
}
//four of kind
for (int i = 1; i <= 13; i++)
if (num[i] == 4) return 1;
for (int i = 1; i <= 13; i++) {
if (num[i] == 2) two++;
if (num[i] == 3) three++;
}
//full house
if (two && three) return 2;
//flush
for (int i = 1; i <= 4; i++)
if (col[i] == 5) return 3;
//straight
st = 0;
for (int i = 1; i <= 13; i++)
if (num[i]) {
st++;
if (st == 5) return 4;
if (st == 4 && num[0]) return 4;
}
else st = 0;
//three of a kind
if (three) return 5;
// tow pairs
if (two > 1) return 6;
//one pair
if (two) return 7;
//high card
return 8;
}
void dfs(int now) {
if (now >= 5) {
int t = search();
if (ans > t) ans = t;
return;
}
int t1, t2;
t1 = value(hand[now][0]);
t2 = color(hand[now][1]);
num[t1]++;
col[t2]++;
dfs(now+1);
num[t1]--;
col[t2]--;
t1 = value(deck[n][0]);
t2 = color(deck[n][1]);
num[t1]++;
col[t2]++;
n++;
dfs(now+1);
num[t1]--;
col[t2]--;
n--;
}
int main() {
while (cin >> hand[0]) {
memset(num, 0, sizeof(num));
memset(col, 0, sizeof(col));
cout << "Hand: ";
for (int i = 1; i < 5; i++)
cin >> hand[i];
for (int i = 0; i < 5; i++)
cout << hand[i] << " ";
cout << "Deck: ";
for (int i = 0; i < 5; i++) {
cin >> deck[i];
cout << deck[i] << " ";
}
ans = 10;
n = 0;
dfs(0);
cout << "Best hand: ";
cout << a[ans] << endl;
}
return 0;
}