题解:P1820 麻将 加强加强版

· · 题解

输入处理:读取牌的范围n和手牌数量k,并统计每张牌的出现次数。 候选牌生成:遍历每个可能的牌x,检查加入x后总牌数是否满足和牌条件。 雀头候选检查:生成加入x后的新牌数数组,收集所有可能的雀头候选。 和牌检查:对于每个雀头候选,扣除两张该牌后,使用贪心策略检查剩余牌是否能组成若干面子。 结果输出:输出所有可和牌的候选牌,若没有则输出"QAQ"。

include <bits/stdc++.h>

using namespace std;

bool check(const vector<int>& count, int n) { vector<int> temp(count.begin(), count.end()); for (int i = 1; i <= n; ++i) { if (temp[i] < 0) return false; int t = temp[i] / 3; temp[i] -= t * 3; int r = temp[i]; if (r > 0) { if (i + 2 > n) return false; if (temp[i+1] < r || temp[i+2] < r) return false; temp[i+1] -= r; temp[i+2] -= r; temp[i] = 0; } } for (int i = 1; i <= n; ++i) { if (temp[i] != 0) return false; } return true; }

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

int n, k;
cin >> n >> k;
vector<int> a(k);
vector<int> count_old(n + 3, 0);
for (int& num : a) {
    cin >> num;
    count_old[num]++;
}

vector<int> valid_x;

for (int x = 1; x <= n; ++x) {
    int total = k + 1;
    if ((total - 2) % 3 != 0)
        continue;

    vector<int> count_new(count_old);
    count_new[x]++;
    vector<int> candidates;
    for (int i = 1; i <= n; ++i) {
        if (count_new[i] >= 2) {
            candidates.push_back(i);
        }
    }

    bool found = false;
    for (int i : candidates) {
        vector<int> temp(count_new);
        temp[i] -= 2;
        if (temp[i] < 0)
            continue;
        if (check(temp, n)) {
            found = true;
            break;
        }
    }

    if (found)
        valid_x.push_back(x);
}

if (valid_x.empty()) {
    cout << "QAQ\n";
} else {
    sort(valid_x.begin(), valid_x.end());
    for (int x : valid_x) {
        cout << x << ' ';
    }
    cout << '\n';
}

return 0;

}