【带模拟】【双端队列】solution of AcWing117 占扑 DIY

题目链接


solution

题目意思题面里说的很清楚了,这里就不再赘述。

说一个坑,就是这句话

当发现四条命都死了以后,统计现在每堆牌上边正面朝上的牌的数目

这个地方想要表达的是只要是正面向上的牌都统计,而不是只统计每堆顶上的牌

然后这个题,我们可以发现她的要求若想实现,那么不仅要实现“插入队头”,还要实现“删去队尾”“删去队头”等操作。这个可以用链表维护,但是更简单且行之有效的方法是使用 STL 里的双端队列 deque 。

由于这是个细节较多的模拟题,直接来看代码。细节在代码中说。

#include <iostream>
#include <cstdio>
#include <queue>
#include <queue>
#include <deque>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;
// 第一关键字存放具体的牌,第二关键字表示是否正面向上

deque<PII > q[105];

void ctn(char c, int i, int j)
{
    if (c == '0') q[i].push_back({10, 0});
    else if (c == 'A') q[i].push_back({1, 0});
    else if (c == 'J') q[i].push_back({11, 0});
    else if (c == 'Q') q[i].push_back({12, 0});
    else if (c == 'K') q[i].push_back({13, 0});
}

int main()
{
    for (int i = 1; i <= 13; i++)
        for (int j = 1; j <= 4; j++)
        {
            char ch; cin >> ch;
            if (ch >= '2' && ch <= '9') q[i].push_back({ch - '0', 0});
            else ctn(ch, i, j);
        }
    int level = 4;	// 生命值
    int d = 13;		// 当前所在的牌堆
    int step = 1;	// 执行哪种操作(生命牌、非生命牌)
    while (level)
    {
        int p; if (step == 1) p = q[d].front().first; else p = q[d].back().first; 
        if (step == 1) q[d].pop_front();
        else q[d].pop_back();
        // 两种取牌的方式
        d = p;
        if (p == 13) {--level; step = 1;}	// 遇到 K
        else
        {
            q[d].push_front({p, 1});
            step = 2;
        }
    }
    int cnt[15] = {}, ans = 0;
    for (int i = 1; i <= 13; i++)
    {
        while (!q[i].empty())	// ! 每一个正面向上的都要统计
        {
            int v = q[i].front().first, c = q[i].front().second;
            if (v != 13 && c) cnt[v]++;
            q[i].pop_front();
        }
    }
    for (int i = 1; i <= 14; i++)
        if (cnt[i] == 4) ++ans;
    cout << ans << "\n";
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值