UVA 131 The Psychic Poker Player

来源:互联网 发布:德州扑克平台 知乎 编辑:程序博客网 时间:2024/05/17 08:11

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 6S2H 2S 3H 3S 3C 2D 3D 6C 9C TH2H 2S 3H 3S 3C 2D 9C 3D 6C TH2H AD 5H AC 7H AH 6H 9H 4H 3CAC 2D 9C 3S KD 5S 4D KS AS 4CKS AH 2H 3C 4H KC 2C TC 2D ASAH 2C 9S AD 3C QH KS JS JD KD6C 9C 8C 2D 7C 2H TC 4C 9S AH3D 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-flushHand: 2H 2S 3H 3S 3C Deck: 2D 3D 6C 9C TH Best hand: four-of-a-kindHand: 2H 2S 3H 3S 3C Deck: 2D 9C 3D 6C TH Best hand: full-houseHand: 2H AD 5H AC 7H Deck: AH 6H 9H 4H 3C Best hand: flushHand: AC 2D 9C 3S KD Deck: 5S 4D KS AS 4C Best hand: straightHand: KS AH 2H 3C 4H Deck: KC 2C TC 2D AS Best hand: three-of-a-kindHand: AH 2C 9S AD 3C Deck: QH KS JS JD KD Best hand: two-pairsHand: 6C 9C 8C 2D 7C Deck: 2H TC 4C 9S AH Best hand: one-pairHand: 3D 5S 2H QD TD Deck: 6S KH 9H AD QH Best hand: highest-card
主要是题目长,其实就是讲德州扑克。只要知道了大小的比较规则就很简单了,直接枚举即可。
按大小来
straight-flush   同花顺
four-of-a-kind   炸弹
full-house      满堂红 三张同点牌加上一对 
flush      同花
straight  顺子(注意A即可接2也可以接K)
three-of-a-kind   三张相同的牌 
two-pairs  两对对子 
one-pair  一对对子 
highest-card     单张最大
#include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <string>using namespace std;string s[10], a[5];string ss[10] = { "straight-flush", "four-of-a-kind", "full-house", "flush", "straight", "three-of-a-kind", "two-pairs", "one-pair", "highest-card" };int i, j, k, w, b[5], c[5];int work(){int i, th, sz;for (i = 0; i < 5; i++){b[i] = 0;if (a[i][0] == 'T') b[i] = 10;if (a[i][0] == 'J') b[i] = 11;if (a[i][0] == 'Q') b[i] = 12;if (a[i][0] == 'K') b[i] = 13;if (a[i][0] == 'A') b[i] = 14;if (!b[i]) b[i] = a[i][0] - '0';if (a[i][1] == 'C') c[i] = 1;if (a[i][1] == 'D') c[i] = 2;if (a[i][1] == 'H') c[i] = 3;if (a[i][1] == 'S') c[i] = 4;}for (i = 0; i < 4; i++)for (j = i + 1; j < 5; j++)if (b[i]>b[j]){swap(b[i], b[j]),swap(c[i], c[j]);}else if (b[i] == b[j] && c[i]>c[j]) swap(c[i], c[j]);for (th = 1, i = 0; i < 4; i++) if (c[i] != c[i + 1]) th = 0;for (sz = 1, i = 0; i < 4; i++)if (b[i] + 1 != b[i + 1]) { if (i == 3 && b[i + 1] == 14 && b[0] == 2) continue; sz = 0; }if (th&&sz) return 0;if (b[1] == b[3] && (b[1] == b[0] || b[3] == b[4])) return 1;if (b[0] == b[1] && b[2] == b[4]) return 2;if (b[0] == b[2] && b[3] == b[4]) return 2;if (th) return 3;if (sz) return 4;if (b[0] == b[2] || b[1] == b[3] || b[2] == b[4]) return 5;if (b[0] == b[1] && (b[2] == b[3] || b[3] == b[4])) return 6;if (b[1] == b[2] && b[3] == b[4]) return 6;if (b[0] == b[1] || b[1] == b[2] || b[2] == b[3] || b[3] == b[4]) return 7;return 8;}int main(){while (cin>>s[0]){for (i = 1; i < 10; i++) cin >> s[i];for (w = 8, i = 0; i < 32; i++){for (j = 0, k = 0; j < 5; j++)if (((i >> j) & 1) == 1) a[k++] = s[j];for (j = 5; j < 10 && k < 5; j++) a[k++] = s[j];w = min(w, work());}cout << "Hand: " ;for (i = 0; i < 5; i++) cout << s[i] << " ";cout << "Deck: " ;for (i = 5; i < 10; i++) cout << s[i] << " ";cout << "Best hand: " << ss[w] << endl;}return 0;}


1 0
原创粉丝点击