您的位置:首页 > 其它

uva 131 The Psychic Poker Player(直接枚举)

2013-08-11 21:28 363 查看


 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 same number of cards from the top of the deck (which is face down). The
object is to maximize the value of the final hand. The different values of hands in poker are given at the end of this problem.

Normally the player cannot see the cards in the deck and so must use probability to decide which cards to discard. In this problem, we imagine that the poker player is psychic and knows which cards are on top of the deck. Write a program which advises the player
which cards to discard so as to maximize the value of the resulting hand.


Input and Output

Input will consist of a series of lines, each containing the initial five cards in the hand then the first five cards on top of the deck. Each card is represented as a two-character code. The first character is the face-value (A=Ace, 2-9, T=10, J=Jack, Q=Queen,
K=King) and the second character is the suit (C=Clubs, D=Diamonds, H=Hearts, S=Spades). Cards will be separated by single spaces. Each input line will be from a single valid deck, that is there will be no duplicate cards in each hand and deck.

Each line of input should produce one line of output, consisting of the initial hand, the top five cards on the deck, and the best value of hand that is possible. Input is terminated by end of file.

Use the sample input and output as a guide. Note that the order of the cards in the player's hand is irrelevant, but the order of the cards in the deck is important because the discarded cards must be replaced from the top of the deck. Also note that examples
of all types of hands appear in the sample output, with the hands shown in decreasing order of value.


Sample Input

TH JH QC QD QS QH KH AH 2S 6S
2H 2S 3H 3S 3C 2D 3D 6C 9C TH
2H 2S 3H 3S 3C 2D 9C 3D 6C TH
2H AD 5H AC 7H AH 6H 9H 4H 3C
AC 2D 9C 3S KD 5S 4D KS AS 4C
KS AH 2H 3C 4H KC 2C TC 2D AS
AH 2C 9S AD 3C QH KS JS JD KD
6C 9C 8C 2D 7C 2H TC 4C 9S AH
3D 5S 2H QD TD 6S KH 9H AD QH



Sample Output

Hand: TH JH QC QD QS Deck: QH KH AH 2S 6S Best hand: straight-flush
Hand: 2H 2S 3H 3S 3C Deck: 2D 3D 6C 9C TH Best hand: four-of-a-kind
Hand: 2H 2S 3H 3S 3C Deck: 2D 9C 3D 6C TH Best hand: full-house
Hand: 2H AD 5H AC 7H Deck: AH 6H 9H 4H 3C Best hand: flush
Hand: AC 2D 9C 3S KD Deck: 5S 4D KS AS 4C Best hand: straight
Hand: KS AH 2H 3C 4H Deck: KC 2C TC 2D AS Best hand: three-of-a-kind
Hand: AH 2C 9S AD 3C Deck: QH KS JS JD KD Best hand: two-pairs
Hand: 6C 9C 8C 2D 7C Deck: 2H TC 4C 9S AH Best hand: one-pair
Hand: 3D 5S 2H QD TD Deck: 6S KH 9H AD QH Best hand: highest-card

题目大意:没有玩过的可能会难懂一些,这里推荐一篇解题报告,看一下会帮助理解题意http://blog.sina.com.cn/s/blog_644010f50100keu6.html
解题思路:枚举出所有持牌情况,注意手上的牌可以选择性的丢弃,牌堆的牌只能按顺序取。既然下来就是判断类型。

可以先统计下总的出现花色种类个数,不同的牌种类个数,在找出个数最多的数值牌个数, 最后判断一下是否有顺子,(A2345,TJQKA都是顺子)

#include <stdio.h>
#include <string.h>

const int N = 20;
const int M = 5;
const char answer
[2 * N] = {"highest-card", "one-pair", "two-pairs", "three-of-a-kind", "straight", "flush", "full-house", "four-of-a-kind", "straight-flush"};
const int bas_two[M] = {16, 8, 4, 2, 1};

struct Card {
int number;
int sex;
char name[M];
}hand
;

int is_number(char c) {
if(c > '1' && c <= '9')
return c - '0';
else if (c == 'T')
return 10;
else if (c == 'J')
return 11;
else if (c == 'Q')
return 12;
else if (c == 'K')
return 13;
else if (c == 'A')
return 1;
else
return 0;
}

int is_sex(char c) {
if (c == 'C')
return 0;
else if (c == 'D')
return 1;
else if (c == 'H')
return 2;
else if (c == 'S')
return 3;
}

void handle(int cur, char str[]) {
strcpy(hand[cur].name, str);
hand[cur].number = is_number(str[0]);
hand[cur].sex = is_sex(str[1]);
}

bool maxLink_num(int num[]) {
for (int i = 1; i < 10; i++) {
if (num[i] > 1)
return false;
if (num[i] == 1) {
int flag = 1;
for (int j = 0; j < M; j++) {
if (num[j + i] != 1) {
flag = 0;
break;
}
}
if (flag)
return true;
}
}

if (num[10] == 1 && num[11] == 1 && num[12] == 1 && num[13] == 1 && num[1] == 1)
return true;
return false;
}

int judge(int cur) {
int A
, B
, C
;
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(C, 0, sizeof(C));
for (int i = 0; i < M; i++) {
if (cur / bas_two[i])
B[i] = 0;
else
B[i] = 1;
cur = cur % bas_two[i];
}

int cnt = 0;
for (int i = 0; cnt < M; i++) {
if (!B[i]) {
C[hand[i].sex]++;
A[hand[i].number]++;
cnt++;
}
}

// Judge color and num;
int flag_color = 0, flag_num = 0;
for (int i = 1; i < 14; i++) {
if (A[i])
flag_num++;
}
for (int i = 0; i < 4; i++) {
if(C[i])
flag_color++;
}
bool link = maxLink_num(A);
int max = 0;
for (int i = 1; i < 14; i++)
if (A[i] > max)
max = A[i];
// printf("link:%d, flag_color:%d, max:%d, flag_num:%d\n", link, flag_color, max, flag_num);

if (flag_color == 1 && link)
return 8;
else if (max == 4)
return 7;
else if (max == 3 && flag_num == 2)
return 6;
else if (flag_color == 1)
return 5;
else if (link)
return 4;
else if (max == 3)
return 3;
else if (max == 2 && flag_num == 3)
return 2;
else if (max == 2)
return 1;
else
return 0;
}

int main() {
int n = 0, max = 0, flag = 0;
char str
;
while (1) {

for (int i = 0; i < 10; i++) {
if (scanf("%s", str) != 1) {
flag = 1;
break;
}
handle(i, str);
}
if (flag) break;

for (int i = 0; i < 32; i++) {
flag = judge(i);
if (flag > max)
max = flag;
}

// Printf;
printf("Hand: ");
for (int i = 0; i < 5; i++)
printf("%s ", hand[i].name);
printf("Deck: ");
for (int i = 0; i < 5; i++)
printf("%s ", hand[i + M].name);
printf("Best hand: %s\n", answer[max]);

// Init;
memset(hand, 0, sizeof(hand));
n = max = flag = 0;
}
return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: