week6 限时大模拟 A - 掌握魔法の东东 II

题意

在这里插入图片描述


思路

创建一个pair< int int >类型的数组a,用来保存一副牌的花色以及大小
运用stl的vector,来存储手牌shoupai,随后使用dfs搜索,数组a里的牌在手牌中只存在两种状态,被选入或者未被选入,所以使用vector的push_back和pop_back让一张牌加入或弹出手牌
对dfs进行剪枝的操作,当手牌术满5张时就可以回溯
对于同花顺 顺子 同花 炸弹 三代二 两对 三条 一对 要不起的判断,将手牌排序好之后,用两个数组来保存花色以及不同大小的牌的个数,通过比较个数来判断 一手牌


总结

写着几次模拟题下来,感觉做题有了思路,但是码力还是不够,写个dfs的剪枝还有记个数来判断花色用了好长的时间,考试凉凉预警,还有最后判断要不起的时候居然把同花给忘记了,WA了两发。认真对待作业和模拟考,认真对待作业和模拟考,认真对待作业和模拟考。


代码

#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

int A,B,size;
int ans[9];//同花顺 顺子 同花 炸弹 三代二 两对 三条 一对 要不起
pair<int,int> a[400];
vector<pair<int,int> > shoupai;

bool cmp(const pair<int,int> &x,const pair<int,int> &b)
{
    return x.first<b.first;
}

// int jishu = 0;
void kind(vector<pair<int,int> > &temp)
{
    // printf("    %d:\n",jishu);
    // for(int i = 0; i < shoupai.size() ; i++)
    // {
    //     printf("%d ",temp[i].first);
        
    // }
    // jishu++;
    // printf("\n");
    sort(temp.begin(),temp.end(),cmp);
    int daxiao[A+1],huase[B+1],dcunt[6];//记录手牌中的大小和花色 记录1张,2张,3张,5张牌相同大小的牌的个数
    for (int i = 0; i < A; i++) daxiao[i] = 0;
    for (int i = 0; i < B; i++) huase[i] = 0;
    for (int i = 0; i < 6; i++) dcunt[i] = 0;

    bool tonghua = false,shunzi = false;
    for (vector<pair<int,int> >::iterator it = temp.begin();it!=temp.end();it++)
    {
        daxiao[it->first]++;
        huase[it->second]++;
    }
    for (int i = 0; i < A; i++) dcunt[daxiao[i]]++;

    int c = 0;//记录顺子
    for (int i = 0 ;i < A; i++){
        while ((i + 1) < A && daxiao[i] == 1){
            if (daxiao[i] == 1 && daxiao[i + 1] == 1){
                c++;
            }
            i++;
        }
    }
    if (c == 4) shunzi = true;
    for (int i = 0; i < B ; i++) if(huase[i] == 5) tonghua = true;

    if (shunzi == true && tonghua == true) ans[0]++;//同花顺
    if (shunzi == true && tonghua == false) ans[1]++;//顺子
    if (shunzi == false && tonghua == true) ans[2]++;//同花
    if (dcunt[4] == 1 || dcunt[5] == 1) ans[3]++;//炸弹
    if (dcunt[3] == 1 && dcunt[2] == 1) ans[4]++;// 三代二
    if (dcunt[2] == 2) ans[5]++;//两对
    if (dcunt[3] == 1 && dcunt[2] != 1) ans[6]++;//三条
    if (dcunt[2] == 1 && dcunt[3] != 1) ans[7]++;//一对
    if (dcunt[1] == 5 && shunzi == false && tonghua == false) ans[8]++;//要不起
}


void judge(pair<int,int> x,pair<int,int> y,int i,vector<pair<int,int> > &shoupai)
{
    if(i > size) return;
    if(shoupai.size() > 5) return;
    if(shoupai.size() == 5) 
    {
        vector<pair<int,int> > temp(shoupai);
        kind(temp);
        return;
    }
    //选择手牌
    shoupai.push_back(a[i]);
    judge(x,y,i+1,shoupai);
    shoupai.pop_back();
    judge(x,y,i+1,shoupai);
}

int main()
{
    scanf("%d%d",&A,&B);
    size = 0;
    pair<int,int> x,y;
    scanf("%d%d",&x.first,&x.second);
    scanf("%d%d",&y.first,&y.second);
    for(int i = 0 ; i < A ; i++)
    {
        for (int j = 0 ; j < B; j++)
        {
            pair<int,int> s;
            s.first = i;
            s.second = j;
            if (s != x && s!= y)
            {
                a[size] = s;
                size++;
            }
        }
    }
    shoupai.push_back(x);
    shoupai.push_back(y);
    judge(x,y,0,shoupai);
    for (int i = 0; i < 9; i++)
    {
        printf("%d ",ans[i]);
    }
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值