8008: 纸牌游戏( "Accordian" Patience)

8008: 纸牌游戏( "Accordian" Patience)

题面

思路

用栈数组模拟,注意寻找pos的左边第一个和左边第三个下标的写法。

示例代码

#include<bits/stdc++.h>

using namespace std;

#define ll long long
//#define int ll
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define fer(i, m, n) for(int i = m; i < n; ++i)
#define ferd(i, m, n) for(int i = m; i >= n; --i)

const int MOD = 1e9 + 7;
const int N = 55;
const int inf = 1e9;

stack<string> stk[N];

bool InRange(int x){
    return x >= 1 && x <= 52;
}

int GetLeftPos1(int x){
    if(InRange(x - 1) && !stk[x - 1].empty()) return x - 1;
    if(InRange(x - 1)) return GetLeftPos1(x - 1);
    return -1;
}

int GetLeftPos3(int x){
    return GetLeftPos1(GetLeftPos1(GetLeftPos1(x)));
}

void Init(){
    fer(i, 1, 53){
        while(!stk[i].empty()) stk[i].pop();
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    int pos = 1;

    while(cin >> s, s!= "#"){
        Init();
        pos = 1;
        stk[1].push(s);
        fer(i, 2, 53){
            cin >> s;
            stk[i].push(s);
        }
        while(pos <= 52){
            if(stk[pos].empty()){
                pos++;
                continue;
            }
            string top = stk[pos].top();
            if(GetLeftPos3(pos) != -1){
                string top1 = stk[GetLeftPos3(pos)].top();
                if(top1[0] == top[0] || top1[1] == top[1]){
                    stk[GetLeftPos3(pos)].push(top);
                    int ind = GetLeftPos3(pos);
                    stk[pos].pop();
                    pos = ind;
                    continue;
                }
            }
            if(GetLeftPos1(pos) != -1){
                string top1 = stk[GetLeftPos1(pos)].top();
                if(top1[0] == top[0] || top1[1] == top[1]){
                    stk[GetLeftPos1(pos)].push(top);
                    int ind = GetLeftPos1(pos);
                    stk[pos].pop();
                    pos = ind;
                    continue;
                }
            }
            pos++;
        }
        int cnt = 0;
        fer(i, 1, 53) cnt += !stk[i].empty();
        if(cnt > 1) cout << cnt << " piles remaining: ";
        else cout << cnt << " pile remaining: ";
        fer(i, 1, 53){
            if(!stk[i].empty()) cout << stk[i].size() << ' ';
        }
        cout << '\n';
    }

    return 0;
}
posted @   Thin_time  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示