Week6Ex:赌神——模拟

题目内容
东东有 A × B 张扑克牌。每张扑克牌有一个大小(整数,记为a,范围区间是 0 到 A - 1)和一个花色(整数,记为b,范围区间是 0 到 B - 1。
扑克牌是互异的,也就是独一无二的,也就是说没有两张牌大小和花色都相同。
“一手牌”的意思是你手里有5张不同的牌,这 5 张牌没有谁在前谁在后的顺序之分,它们可以形成一个牌型。 我们定义了 9 种牌型,如下是 9 种牌型的规则,我们用“低序号优先”来匹配牌型,即这“一手牌”从上到下满足的第一个牌型规则就是它的“牌型编号”(一个整数,属于1到9):

同花顺: 同时满足规则 2 和规则 3.
顺子 : 5张牌的大小形如 x, x + 1, x + 2, x + 3, x + 4
同花 : 5张牌都是相同花色的.
炸弹 : 5张牌其中有4张牌的大小相等.
三带二 : 5张牌其中有3张牌的大小相等,且另外2张牌的大小也相等.
两对: 5张牌其中有2张牌的大小相等,且另外3张牌中2张牌的大小相等.
三条: 5张牌其中有3张牌的大小相等.
一对: 5张牌其中有2张牌的大小相等.
要不起: 这手牌不满足上述的牌型中任意一个.
现在, 东东从A × B 张扑克牌中拿走了 2 张牌!分别是 (a1, b1) 和 (a2, b2). (其中a表示大小,b表示花色)
现在要从剩下的扑克牌中再随机拿出 3 张!组成一手牌!!
其实东东除了会打代码,他业余还是一个魔法师,现在他要预言他的未来的可能性,即他将拿到的“一手牌”的可能性,我们用一个“牌型编号(一个整数,属于1到9)”来表示这手牌的牌型,那么他的未来有 9 种可能,但每种可能的方案数不一样。
现在,东东的阿戈摩托之眼没了,你需要帮他算一算 9 种牌型中,每种牌型的方案数。

输入格式
第 1 行包含了整数 A 和 B (5 ≤ A ≤ 25, 1 ≤ B ≤ 4).

第 2 行包含了整数 a1, b1, a2, b2 (0 ≤ a1, a2 ≤ A - 1, 0 ≤ b1, b2 ≤ B - 1, (a1, b1) ≠ (a2, b2)).

输出格式
输出一行,这行有 9 个整数,每个整数代表了 9 种牌型的方案数(按牌型编号从小到大的顺序)

输入案例

5 2
1 0 3 1

输出案例

0 8 0 0 0 12 0 36 0

输入案例

25 4
0 0 24 3

输出案例

0 0 0 2 18 1656 644 36432 113344

思路
思路其实很简单…就是判断就是了。
已经抽到手了两张牌,且这两张牌固定,所以只需要遍历另外三张牌的所有可能,然后对每一次可能进行判断就行了。
判断有很多种方式,比如数组记录各种符合条件的牌的数量。
这里使用最暴力的判断方法(反正时间给了2秒)

其实最后也就花了31ms,尽管在自己的IDE上跑的是500ms左右。

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <time.h>

using namespace std;

int A, B;
int a1, b1, a2, b2;
int opnum[10];

int as[5], bs[5];

struct Card
{
    int a;//牌号
    int b;//花色
    Card(int a,int b):a(a),b(b){}
};

vector<Card>pile;
vector<Card>hand;

void Init()
{
    pile.clear();
    hand.clear();

    for(int i=0;i<A;i++)//牌号
        for (int j = 0; j < B; j++)//花色
        {
            if (((i == a1) && (j == b1)) || ((i == a2) && (j == b2)))
            {
                hand.push_back(Card(i, j));//塞进手牌
            }
            else
            {
                pile.push_back(Card(i, j));//塞进牌堆
            }
        }
}

bool cmp(const Card& m, const Card& n)//按照大小排序,然后再按花色排序
{
    if (m.a != n.a) return m.a < n.a;
    else return m.b < n.b;
}

void Judge(vector<Card> now)//暴力判断
{
    sort(now.begin(), now.end(), cmp);

    bool flag2 = false, flag3 = false;

    for (int i = 0; i < 5; i++)//Mini_Init
    {
        as[i] = now[i].a;
        bs[i] = now[i].b;
    }

    if (as[1] == as[0] + 1 && as[2] == as[0] + 2 && as[3] == as[0] + 3 && as[4] == as[0] + 4) flag2 = true;
    if (bs[0] == bs[1] && bs[1] == bs[2] && bs[2] == bs[3] && bs[3] == bs[4]) flag3 = true;

    if (flag2 && flag3) { opnum[1]++; return; }//1.同花顺
    if (flag2) { opnum[2]++; return; }//2.顺子
    if (flag3) { opnum[3]++; return; }//3.同花

    if (as[0] != as[1] && as[1] != as[2] && as[2] != as[3] && as[3] != as[4])
    {
        opnum[9]++; return;//9.要不起
    }

    if ((as[0] == as[1] && as[1] == as[2] && as[2] == as[3]) || (as[1] == as[2] && as[2] == as[3] && as[3] == as[4]))
    {
        opnum[4]++; return;//4.炸弹
    }

    if ((as[0] == as[1] && as[1] == as[2]))
    {
        if (as[3] == as[4]) { opnum[5]++; return; }//5.三带二
        else { opnum[7]++; return; }//7.三条
    }

    if ((as[2] == as[3] && as[3] == as[4]))
    {
        if(as[0]==as[1]) { opnum[5]++; return; }//5.三带二
        else { opnum[7]++; return; }//7.三条
    }

    if ((as[1] == as[2] && as[2] == as[3])) { opnum[7]++; return; }//7.三条

    if (as[0] == as[1])//6.两对
    {
        if (as[2] == as[3] || as[3] == as[4])
        {
            opnum[6]++; return;
        }
        else//只有一对
        {
            opnum[8]++; return;
        }
    }
    else if (as[1] == as[2] && as[3] == as[4])
    {
        opnum[6]++; return;
    }
    else { opnum[8]++; return; }
    

}

void DrawCard()
{
    for (int i = 0; i < A * B - 2; i++)
    {
        hand.push_back(pile[i]);
        for (int j = i + 1; j < A * B - 2; j++)
        {
            hand.push_back(pile[j]);
            for (int k = j + 1; k < A * B - 2; k++)
            {
                hand.push_back(pile[k]);
                Judge(hand);
                hand.pop_back();
            }
            hand.pop_back();
        }
        hand.pop_back();
    }
}

int main()
{
    memset(opnum, 0, sizeof opnum);
    memset(as, 0, sizeof as);
    memset(bs, 0, sizeof bs);

    cin >> A >> B;
    cin >> a1 >> b1 >> a2 >> b2;
    Init();

    //clock_t start, finish;
    //start = clock();

    DrawCard();

    for (int i = 1; i <= 9; i++)
    {
        cout << opnum[i] << ' ';
    }

    //finish = clock();
    //cout << endl << "反应时间为:" << finish - start << "ms" << endl;

    return 0;
}
发布了21 篇原创文章 · 获赞 3 · 访问量 405

猜你喜欢

转载自blog.csdn.net/qq_44506233/article/details/105282368