ZOJ1111 - Poker Hands
生活随笔
收集整理的這篇文章主要介紹了
ZOJ1111 - Poker Hands
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Problem : Poker Hands
Description :
兩個(gè)人玩牌,每個(gè)人五張撲克牌,2最小,A最大。規(guī)則如下:
- High Card. Hands which do not fit any higher category are ranked by the value of their highest card. If the highest cards have the same value, the hands are ranked by the next highest, and so on.
- 最低等級(jí),降序排序,然后按照字典序比較。
- Pair. 2 of the 5 cards in the hand have the same value. Hands which both contain a pair are ranked by the value of the cards forming the pair. If these values are the same, the hands are ranked by the values of the cards not forming the pair, in decreasing order.
- 一對(duì),兩個(gè)牌的數(shù)字相同,先按對(duì)子的值比,如果一樣,就把其余的三個(gè)降序排序,字典序比較。
- Two Pairs. The hand contains 2 different pairs. Hands which both contain 2 pairs are ranked by the value of their highest pair. Hands with the same highest pair are ranked by the value of their other pair. If these values are the same the hands are ranked by the value of the remaining card.
- 兩個(gè)一對(duì),按最大的那個(gè)對(duì)子比,如果一樣就按第二大的,還是一樣,就按剩下的那張牌比較。
- Three of a Kind. Three of the cards in the hand have the same value. Hands which both contain three of a kind are ranked by the value of the 3 cards.
- 三角, 三個(gè)牌的數(shù)字相同,就按這三個(gè)數(shù)字的值比。
- Straight. Hand contains 5 cards with consecutive values. Hands which both contain a straight are ranked by their highest card.
- 順子, 五個(gè)牌的數(shù)字連續(xù)。按最大的那個(gè)牌比。
- Flush. Hand contains 5 cards of the same suit. Hands which are both flushes are ranked using the rules for High Card.
- 同花, 五個(gè)牌的花色都一樣,按照最低等級(jí)的規(guī)則比較。
- Full House. 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.
- 馬, 由順子和一對(duì)組成,按直線比。
- Four of a kind. 4 cards with the same value. Ranked by the value of the 4 cards.
- 四角,四個(gè)牌的數(shù)字一樣,按這四個(gè)數(shù)字比。
- Straight flush. 5 cards of the same suit with consecutive values. Ranked by the highest card in the hand.
- 同花順, 五張牌數(shù)字連續(xù)且花色相同。按最大的那個(gè)牌比較。
Solution :
大游戲模擬,最主要是看清楚規(guī)則,翻譯好,唯一用到的小技巧就是13進(jìn)制Hash。翻譯是我亂在翻譯,不過(guò)意思還是可以懂的。
Code (C) :
#include <stdio.h> #include <string.h> #include <stdlib.h>const char SUIT[] = "CDHS"; const char VALUE[] = "23456789TJQKA";const int p_13[] = {1, 13, 169, 2197, 28561};struct Card {int value, suit; };typedef struct Card Player[5];struct Ans {int cata;int best; };int find_value(char card[]) {for (int i = 0; i < 13; i++)if (card[0] == VALUE[i])return i;return -1; }int find_suit(char card[]) {for (int i = 0; i < 4; i++)if (card[1] == SUIT[i])return i;return -1; }int four_kind(Player p, int *value) {int v[13] = {0};for (int i = 0; i < 5; i++)v[p[i].value]++;for (int i = 0; i < 13; i++)if (v[i] >= 4) {*value = i;return 1;}return 0; }int full_house(Player p, int *value) {int v[13] = {0}, s[13] = {0};int tmp = -1;for (int i = 0; i < 5; i++)v[p[i].value]++;for (int i = 0; i < 13; i++)if (v[i] >= 3) {tmp = i;break;}if (tmp == -1)return 0;for (int i = 0; i < 5; i++)if (tmp != p[i].value)++s[p[i].value];for (int i = 0; i < 13; i++)if (s[i] >= 2) {*value = tmp;return 1;}return 0; }int conse(Player p) {for (int i = 0; i < 4; i++)for (int j = 0; j < 4 - i; j++)if (p[j].value > p[j + 1].value) {struct Card tmp = p[j];p[j] = p[j + 1];p[j + 1] = tmp;}for (int i = 1; i < 5; i++)if (p[i].value != p[i - 1].value + 1)return 0;return 1; }int three_kind(Player p, int *value) {int v[13] = {0};for (int i = 0; i < 5; i++)v[p[i].value]++;for (int i = 0; i < 13; i++)if (v[i] >= 3) {*value = i;return 1;}return 0; }int hig(Player p) {int ret = -1;for (int i = 0; i < 5; i++)if (ret < p[i].value)ret = p[i].value;return ret; }int one(Player p) {int ret = 0;conse(p);for (int i = 4; i >= 0; i--)ret += p[i].value * p_13[i];return ret; }int pair(Player p, int *value) {int v[13] = {0}, ret = 0, pos;int s[5], top = 0;for (int i = 0; i < 5; i++)++v[p[i].value];for (int i = 0; i < 13; i++)if (v[i] >= 2) {pos = i;ret = 1;} else if (v[i])s[top++] = i;if (ret == 0)return 0;for (int i = 0; i < top - 1; i++)for (int j = 0; j < top - 1 - i; j++)if (s[j] < s[j + 1]) {int tmp = s[j];s[j] = s[j + 1];s[j + 1] = tmp;}*value = pos * p_13[3] + s[0] * p_13[2] + s[1] * p_13[1] + s[2];return 1; }int two_pair(Player p, int *value) {int v[13] = {0}, m = 0;int s[2], top = 0, other;for (int i = 0; i < 5; i++)++v[p[i].value];for (int i = 0; i < 13; i++)if (v[i] >= 2) {++m;s[top++] = i;} else if (v[i])other = i;if (m != 2)return 0;if (s[0] < s[1]) {int tmp = s[0];s[0] = s[1];s[1] = tmp;}*value = s[0] * p_13[2] + s[1] * p_13[1] + other;return 1; }struct Ans judge(Player p) {int value;struct Ans ret;int C = 0, D = 0, H = 0, S = 0;for (int i = 0; i < 5; i++)if (p[i].suit == 0)++C;else if (p[i].suit == 1)++D;else if (p[i].suit == 2)++H;else++S;if ((C == 5 || D == 5 || H == 5 || S == 5 ) && conse(p)) {ret.cata = 9;ret.best = hig(p);return ret;}if (four_kind(p, &value)) {ret.cata = 8;ret.best = value;return ret;}if (full_house(p, &value)) {ret.cata = 7;ret.best = value;return ret;}if (C == 5 || D == 5 || H == 5 || S == 5) {ret.cata = 6;ret.best = one(p);return ret;}if (conse(p)) {ret.cata = 5;ret.best = hig(p);return ret;}if (three_kind(p, &value)) {ret.cata = 4;ret.best = value;return ret;}if (two_pair(p, &value)) {ret.cata = 3;ret.best = value;return ret;}if (pair(p, &value)) {ret.cata = 2;ret.best = value;return ret;}ret.cata = 1;ret.best = one(p);return ret; }int CMP(struct Ans a, struct Ans b) {if (a.cata != b.cata)return a.cata - b.cata;return a.best - b.best; }int main() {//freopen("in", "r", stdin);char card[3];while (1) {int ret = 0;Player playerA, playerB;struct Ans A, B;int cmp;for (int i = 0; i < 5; i++) {scanf("%s", card);playerA[i].value = find_value(card);playerA[i].suit = find_suit(card);}for (int i = 0; i < 5; i++) {ret = scanf("%s", card);playerB[i].value = find_value(card);playerB[i].suit = find_suit(card);}if (ret == EOF)break;A = judge(playerA);B = judge(playerB);cmp = CMP(A, B);if (!cmp)puts("Tie.");else if (cmp > 0)puts("Black wins.");elseputs("White wins.");}return 0; }總結(jié)
以上是生活随笔為你收集整理的ZOJ1111 - Poker Hands的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [codeforces 1359A]
- 下一篇: CVPR 2022 Oral | 人大高