[yukicoder] No. 832 麻雀修行中

2020年12月15日

問題

方針

全探索を行って和了しているかを調べます。また、七対子は特殊な形なので、\( 7 \) 種類の対子があるのかを調べます。他の手は、\( 4 \) 面子 \( 1 \) 雀頭の形をしているかをチェックすれば良いので、まず初めに雀頭候補から探索し、暗刻と順子を深さ優先で調べます。

コード

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

// a: 手牌 b: 山
int a[9]{};
string S;

void init() {
    fill(a, a + 9, 0);
    for (int i = 0; i < 13; i++) {
        a[S[i] - '1']++;
    }
}


bool check_titoitu() {
    int cnt = 0;
    for (int i = 0; i < 9; i++) {
        if (a[i] == 2) cnt++;
    }
    return cnt == 7 ? true : false;
}

bool flag = false;

void dfs(int n) {
    if (flag) return;
    if (n == 5) {
        flag = true;
        return;
    }
    if (n == 0) {
        for (int i = 0; i < 9; i++) {
            if (a[i] >= 2) {
                a[i] -= 2;
                dfs(n + 1);
                a[i] += 2;
            }
        }
    }
    if (n == 0) return;
    for (int i = 0; i < 9; i++) {
        if (a[i] >= 3) {
            a[i] -= 3;
            dfs(n + 1);
            a[i] += 3;
        }
    }
    for (int i = 0; i + 2 < 9; i++) {
        if (a[i] > 0 && a[i + 1] > 0 && a[i + 2] > 0) {
            a[i]--;
            a[i + 1]--;
            a[i + 2]--;
            dfs(n + 1);
            a[i]++;
            a[i + 1]++;
            a[i + 2]++;
        }
    }
}

int main() {
    cin >> S;
    set<int> s;
    for (int i = 0; i < 9; i++) {
        init();
        if (a[i] != 4) {
            a[i]++;
            if (check_titoitu()) s.insert(i + 1);
            dfs(0);
            if (flag) {
                s.insert(i + 1);
            }
            flag = false;
            a[i]--;
        }
    }
    for (int i : s) {
        cout << i << "\n";
    }
    return 0;
}