UVA246 10-20-30游戏 10-20-30

我佛了,为什么刘汝佳不把题意说完?从7个牌堆里面去掉牌的规则他说的很清楚,但是他没有说要一直把这个牌堆去到不能去为止,所以我写的时候每个牌堆,如果可以去掉的话,就去掉一个就过了,就下一个牌堆了,结果愣是干瞪眼两个小时,实在忍不了了去看看别人写的题解,结果第一句话就是每个牌堆去到不能去为止,这点洛谷也不行,洛谷的题解我老早就看了,就是不说这个规则,不过得益于一直在肉眼找逻辑错误,所以看到这个规则之后很快就把代码改好,毕竟思路已经很清晰了,就是这个坑一直不知道,

说一下几个注意的点把,题目要求输出步数,也就是从手牌堆里取牌的次数,从桌子上放到手里的不算,然后一开始桌子上是没有牌的,我们从手里拿7张放在桌子上形成牌堆,这就已经是7步了,
然后是如何判断形成死循环,因为为了模拟牌堆被删除,所以用了vector来存放双端队列,每个元素就是一个双端队列,vector是可以直接赋值比较大小的,所以不用重载运算符,每次循环的末尾,也就是过了一次操作之后,把牌堆和手牌都存到vector里面然后用map映射判断就行了,
双端队列好像是没有size的,只能判断它们是不是空的,所以你要想知道一个队列里面的元素的个数,需要额外写一个函数循环判断

这一章的两道双端队列感觉都是模拟的比重占的更大一点,难点不在数据结构和算法,就是模拟

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

int getnum(deque<int> q) {
    int cnt = 0;
    while (!q.empty()) { cnt++; q.pop_front(); }
    return cnt;
}

int main() {
    int x;
    while (cin >> x && x) {
        deque<int> hand;
        hand.pb(x);
        for (int i = 1; i < 52; i++) { cin >> x; hand.pb(x); }
        deque<int> q[7];
        for (int i = 0; i < 7; i++) { q[i].pb(hand.front()); hand.pop_front(); }
        vector<deque<int> > v;
        for (int i = 0; i < 7; i++) v.pb(q[i]);
        int ok = 0;
        int d = 0;
        int ans = 0;
        map<vector<deque<int> >, int> mp;
        while (true) {
            ans++;
            v[d].pb(hand.front());
            hand.pop_front();
            while (getnum(v[d]) >= 3) {
                if (getnum(v[d]) == 3) {
                    int x1 = v[d].front(); v[d].pop_front();
                    int x2 = v[d].front(); v[d].pop_front();
                    int x3 = v[d].front(); v[d].pop_front();
                    int xx = x1 + x2 + x3;
                    if (xx == 10 || xx == 20 || xx == 30) {
                        hand.pb(x1); hand.pb(x2); hand.pb(x3);
                    } else {
                        v[d].pb(x1); v[d].pb(x2); v[d].pb(x3);
                        break;
                    }
                } else {
                    int x1 = v[d].front(); v[d].pop_front();
                    int x2 = v[d].front(); v[d].push_front(x1);
                    int x5 = v[d].back(); v[d].pop_back();
                    int x4 = v[d].back(); v[d].pop_back();
                    int x3 = v[d].back(); v[d].pb(x4); v[d].pb(x5);
                    int xx1 = x1 + x2 + x5;
                    int xx2 = x1 + x4 + x5;
                    int xx3 = x3 + x4 + x5;
                    if (xx1 == 10 || xx1 == 20 || xx1 == 30) {
                        hand.pb(x1); hand.pb(x2); hand.pb(x5);
                        v[d].pop_front(); v[d].pop_front(); v[d].pop_back();
                    } else if (xx2 == 10 || xx2 == 20 || xx2 == 30) {
                        hand.pb(x1); hand.pb(x4); hand.pb(x5);
                        v[d].pop_front(); v[d].pop_back(); v[d].pop_back();
                    } else if (xx3 == 10 || xx3 == 20 || xx3 == 30) {
                        hand.pb(x3); hand.pb(x4); hand.pb(x5);
                        v[d].pop_back(); v[d].pop_back(); v[d].pop_back();
                    } else break;
                }
            }
            if (v[d].empty()) {
                v.erase(v.begin() + d);
                if (sz(v) == 0) { ok = 1; break; }
                else if (d == sz(v)) d = 0;
            } else {
                if (d == sz(v) - 1) d = 0;
                else d++;
            }
            if (hand.empty()) { ok = 2; break; }
            vector<deque<int> > vv;
            for (int i = 0; i < sz(v); i++) vv.pb(v[i]);
            vv.pb(hand);
            if (mp.find(vv) != mp.end()) break;
            else mp[vv] = 1;
        }
        if (!ok) cout << "Draw: " << ans + 7 << endl;
        else if (ok == 1) cout << "Win : " << ans + 7 << endl;
        else cout << "Loss: " << ans + 7 << endl;
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值