QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#82054#4858. Poker Game: DecisionSortingAC ✓422ms11600kbC++148.2kb2023-02-27 00:08:532023-02-27 00:10:43

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-27 00:10:43]
  • 评测
  • 测评结果:AC
  • 用时:422ms
  • 内存:11600kb
  • [2023-02-27 00:08:53]
  • 提交

answer

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>

using namespace std;

int alice[2], bob[2];
int shared[6];

pair<int, int> p[14][14][14][14][14][2];

void init(){
    static int a[5];
    for(int a0 = 1; a0 <= 13; ++a0)
        for(int a1 = 1; a1 <= 13; ++a1)
            for(int a2 = 1; a2 <= 13; ++a2)
                for(int a3 = 1; a3 <= 13; ++a3)
                    for(int a4 = 1; a4 <= 13; ++a4){
                        for(int a_flush = 0; a_flush <= 1; ++a_flush){
                            a[0] = a0;
                            a[1] = a1;
                            a[2] = a2;
                            a[3] = a3;
                            a[4] = a4;
                            sort(a, a + 5, greater<int>());

                            bool a_straight = true;
                            for(int i = 0; i < 4; ++i)
                                a_straight &= (a[i] == a[i + 1] + 1) || (a[i] == 13 && a[i + 1] == 4);
                            
                            auto &ans = p[a0][a1][a2][a3][a4][a_flush];

                            int a_score = 0;
                            if(a_straight && a_flush){
                                a_score = a[0];
                                if(a[0] == 13 && a[1] == 4)
                                    a_score = 4;
                            }

                            if(a_score){
                                ans = {9, a_score};
                                continue;
                            }

                            if(a[0] == a[3])
                                a_score = 16 * a[0] + a[4];
                            else if(a[1] == a[4])
                                a_score = 16 * a[1] + a[0];

                            if(a_score){
                                ans = {8, a_score};
                                continue;
                            }

                            if(a[0] == a[2] && a[3] == a[4])
                                a_score = 16 * a[0] + a[3];
                            else if(a[0] == a[1] && a[2] == a[4])
                                a_score = 16 * a[2] + a[0];

                            if(a_score){
                                ans = {7, a_score};
                                continue;
                            }

                            if(a_flush)
                                a_score = 16 * 16 * 16 * 16 * a[0] + 16 * 16 * 16 * a[1] + 16 * 16 * a[2] + 16 * a[3] + a[4];

                            if(a_score){
                                ans = {6, a_score};
                                continue;
                            }

                            if(a_straight){
                                a_score = a[0];
                                if(a[0] == 13 && a[1] == 4)
                                    a_score = 4;
                            }

                            if(a_score){
                                ans = {5, a_score};
                                continue;
                            }

                            if(a[0] == a[2])
                                a_score = 16 * 16 * a[0] + 16 * a[3] + a[4];
                            else if(a[1] == a[3])
                                a_score = 16 * 16 * a[1] + 16 * a[0] + a[4];
                            else if(a[2] == a[4])
                                a_score = 16 * 16 * a[2] + 16 * a[0] + a[1];

                            if(a_score){
                                ans = {4, a_score};
                                continue;
                            }

                            if(a[0] == a[1] && a[2] == a[3])
                                a_score = 16 * 16 * a[0] + 16 * a[2] + a[4];
                            else if(a[0] == a[1] && a[3] == a[4])
                                a_score = 16 * 16 * a[0] + 16 * a[3] + a[2];
                            else if(a[1] == a[2] && a[3] == a[4])
                                a_score = 16 * 16 * a[1] + 16 * a[3] + a[0];

                            if(a_score){
                                ans = {3, a_score};
                                continue;
                            }

                            if(a[0] == a[1])
                                a_score = a[0] * 16 * 16 * 16 + a[2] * 16 * 16 + a[3] * 16 + a[4];
                            else if(a[1] == a[2])
                                a_score = a[1] * 16 * 16 * 16 + a[0] * 16 * 16 + a[3] * 16 + a[4];
                            else if(a[2] == a[3])
                                a_score = a[2] * 16 * 16 * 16 + a[0] * 16 * 16 + a[1] * 16 + a[4];
                            else if(a[3] == a[4])
                                a_score = a[3] * 16 * 16 * 16 + a[0] * 16 * 16 + a[1] * 16 + a[2];

                            if(a_score){
                                ans = {2, a_score};
                                continue;
                            }

                            a_score = a[0] * 16 * 16 * 16 * 16 + a[1] * 16 * 16 * 16 + a[2] * 16 * 16 + a[3] * 16 + a[4];
                            ans = {1, a_score};
                        }
                    }
}

int outcome(int state){
    static int a[5], b[5];
    a[0] = alice[0];
    a[1] = alice[1];
    b[0] = bob[0];
    b[1] = bob[1];

    int cnt_a = 2, cnt_b = 2;
    for(int i = 0; i < 6; ++i){
        if(state >> (2 * i) & 1)
            a[cnt_a++] = shared[i];
        else
            b[cnt_b++] = shared[i];
    }

    // for(int i = 0; i < 5; ++i)
        // cout << (a[i] / 4) << "," << (a[i] % 4) << " ";
    // cout << " a" << endl;
    // for(int i = 0; i < 5; ++i)
        // cout << (b[i] / 4) << "," << (b[i] % 4)<< " ";
    // cout << " b" << endl;

    bool a_flush = true;
    for(int i = 0; i < 4; ++i)
        a_flush &= (a[i] & 3) == (a[i + 1] & 3);
    bool b_flush = true;
    for(int i = 0; i < 4; ++i)
        b_flush &= (b[i] & 3) == (b[i + 1] & 3);

    for(int i = 0; i < 5; ++i){
        a[i] >>= 2;
        b[i] >>= 2;
    }

    auto score_a = p[a[0]][a[1]][a[2]][a[3]][a[4]][a_flush];
    auto score_b = p[b[0]][b[1]][b[2]][b[3]][b[4]][b_flush];
    // cout << score_a.first << "," << score_a.second << " " << score_b.first << "," << score_b.second << " score_a score_b" << endl;
    return (score_a > score_b) - (score_a < score_b);
}

int recurse(int state){
    int turns = __builtin_popcount(state);
    if(turns == 6){
        // cout << state << " - " << outcome(state) << endl;
        return outcome(state);
    }

    int ans;
    bool pl = turns & 1;
    ans = -1 + 2 * pl;

    for(int i = 0; i < 6; ++i){
        if(state >> (2 * i) & 3)
            continue;
        
        int cand = recurse(state + (1 << (2 * i + pl)));
        if(cand == 1 - 2 * pl)
            return cand;

        if(pl)
            ans = min(ans, cand);
        else
            ans = max(ans, cand);
    }
    return ans;
}

int string_to_card(const string &s){
    int ans = 0;
    if('2' <= s[0] && s[0] <= '9')
        ans = s[0] - '0' - 1;
    else if(s[0] == 'T')
        ans = 9;
    else if(s[0] == 'J')
        ans = 10;
    else if(s[0] == 'Q')
        ans = 11;
    else if(s[0] == 'K')
        ans = 12;
    else 
        ans = 13;
    ans *= 4;

    if(s[1] == 'S')
        ans += 0;
    else if(s[1] == 'H')
        ans += 1;
    else if(s[1] == 'C')
        ans += 2;
    else
        ans += 3;
    return ans;
}

void solve(){
    for(int i = 0; i < 2; ++i){
        string s;
        cin >> s;
        alice[i] = string_to_card(s);
    }
    for(int i = 0; i < 2; ++i){
        string s;
        cin >> s;
        bob[i] = string_to_card(s);
    }
    for(int i = 0; i < 6; ++i){
        string s;
        cin >> s;
        shared[i] = string_to_card(s);
    }

    int answer = recurse(0);
    if(answer == 1)
        cout << "Alice\n";
    else if(answer == 0)
        cout << "Draw\n";
    else
        cout << "Bob\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    init();

    int t;
    cin >> t;

    while(t--)
        solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 23ms
memory: 11600kb

input:

9
JC 4H
TS 5D
JS JH JD 4S 4C 4D
JC 4H
TS 5D
TH TC TD 5S 5H 5C
JC 4H
TS 5D
4S JS 5S TH TC TD
7C 3C
7H TH
3S 3H 3D 2C 4H 5S
7C 3C
7H TH
2H 4H 5H 6H 8H 9H
7C 3C
7H TH
TS 3S 2S 2H 4C 4D
2D KH
4D JC
2S 2H 2C KS KC KD
2D KH
4D JC
4S 4H 4C JS JH JD
2D KH
4D JC
2S KS 4S JS JH JD

output:

Alice
Bob
Draw
Alice
Bob
Draw
Alice
Bob
Draw

result:

ok 9 lines

Test #2:

score: 0
Accepted
time: 27ms
memory: 10680kb

input:

1
AS 2H
2S 6H
3S 3H 4S 4H 5S 5H

output:

Bob

result:

ok single line: 'Bob'

Test #3:

score: 0
Accepted
time: 18ms
memory: 11268kb

input:

1
5D 9H
KC 6H
9C QH JH 3C AC 9S

output:

Alice

result:

ok single line: 'Alice'

Test #4:

score: 0
Accepted
time: 422ms
memory: 11160kb

input:

100000
5C JS
KS 7S
4D 2D KH 6S 6D 6H
2C KS
7D 5D
5S AH 3S 9C JH 5H
9D 4D
3H TS
2D 2C 7H 3D AH 3S
KS 8H
7H QD
TH 4H QS 8S 6D TD
JS 8D
4H 2S
AH 2H 2C 6S 4C 8S
JS KD
4S 2D
JH 4C 6S QD AH 7C
9D 4H
QH JS
KH QC 6S 5D JC AD
KD 5D
4C TC
4H 2C TD 7S 6S QH
3C 5C
QC 9D
QH 5D AC 7D 8C 8S
4S 6H
9C 8C
JC 7S QH 6C...

output:

Bob
Bob
Bob
Alice
Bob
Alice
Bob
Bob
Alice
Alice
Alice
Alice
Bob
Alice
Alice
Alice
Alice
Alice
Bob
Bob
Alice
Bob
Bob
Alice
Alice
Alice
Alice
Alice
Alice
Alice
Alice
Alice
Alice
Alice
Alice
Bob
Alice
Alice
Bob
Bob
Alice
Alice
Bob
Bob
Bob
Alice
Alice
Alice
Bob
Bob
Alice
Alice
Alice
Alice
Alice
Alice
Al...

result:

ok 100000 lines