C. Soldier and Cards

https://codeforces.com/problemset/problem/546/C

题意:给定一个不超过10的数n,代表有n张牌,a和b每个人各分几张。然后玩比大小的游戏,问多少回合,谁先赢,或者谁都赢不了?

思路:看到n不超过10,直接暴力了。

总结:没有太明白的搞清楚都赢不了的情况,应该是a和b同时持有相同的牌出现了不止一次时,才可以判定无解。如果只是单个人持有相同的牌两次,不能断定无解。

inline void solve(){
    int n;
    cin >> n;

    int k1, k2;
    cin >> k1;
    deque<int> a;
    for (int i = 0; i < k1; ++i) {
        int t;
        cin >> t;
        a.push_back(t);
    }

    cin >> k2;
    deque<int> b;
    for (int i = 0; i < k2; ++i) {
        int t;
        cin >> t;
        b.push_back(t);
    }

    int cnt = 0;
    set<deque<int>> s;
    set<deque<int>> sb;
    s.insert(a);
    sb.insert(b);
    while (!a.empty() && !b.empty()) {
        if (a.front() > b.front()) {
            a.push_back(b.front());
            b.pop_front();
            a.push_back(a.front());
            a.pop_front();
        }
        else {
            b.push_back(a.front());
            a.pop_front();
            b.push_back(b.front());
            b.pop_front();
        }
        cnt ++;
        if (s.count(a) && sb.count(b)) {
            cout << -1 << '\n';
            return;
        }
        s.insert(a);
        sb.insert(b);
    }

    cout << cnt << " " << (a.empty() ? 2 : 1) << '\n';
}
posted @ 2025-03-08 14:15  _Yxc  阅读(7)  评论(0)    收藏  举报