ZOJ1111 - Poker Hands

Problem : Poker Hands

Description :

两个人玩牌,每个人五张扑克牌,2最小,A最大。规则如下:

  • 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.
    • 最低等级,降序排序,然后按照字典序比较。
  • 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.
    • 一对,两个牌的数字相同,先按对子的值比,如果一样,就把其余的三个降序排序,字典序比较。
  • 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.
    • 两个一对,按最大的那个对子比,如果一样就按第二大的,还是一样,就按剩下的那张牌比较。
  • 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.
    • 三角, 三个牌的数字相同,就按这三个数字的值比。
  • Straight. Hand contains 5 cards with consecutive values. Hands which both contain a straight are ranked by their highest card.
    • 顺子, 五个牌的数字连续。按最大的那个牌比。
  • Flush. Hand contains 5 cards of the same suit. Hands which are both flushes are ranked using the rules for High Card.
    • 同花, 五个牌的花色都一样,按照最低等级的规则比较
  • Full House. 3 cards of the same value, with the remaining 2 cards forming a pair. Ranked by the value of the 3 cards.
    • 马, 由顺子和一对组成,按直线比。
  • Four of a kind. 4 cards with the same value. Ranked by the value of the 4 cards.
    • 四角,四个牌的数字一样,按这四个数字比。
  • Straight flush. 5 cards of the same suit with consecutive values. Ranked by the highest card in the hand.
    • 同花顺, 五张牌数字连续且花色相同。按最大的那个牌比较。

Solution :

大游戏模拟,最主要是看清楚规则,翻译好,唯一用到的小技巧就是13进制Hash。翻译是我乱在翻译,不过意思还是可以懂的。

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.");
        else
            puts("White wins.");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值