HDU 4431 Mahjong

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 37;
const int INF = 0x3f3f3f3f;
int kase, a[maxn], ans[maxn];

///m,s,p,c


bool check43() {
    int cnt[maxn];
    for(int i = 0; i < 34; ++i) cnt[i] = a[i];

    int res = 0;
    for(int i = 0; i < 27; i += 9) {
        for(int j = 0; j < 9; ++j) {
            if(cnt[j+i] >= 3) {
                cnt[j+i] -= 3;
                res++;
            }
            while(j < 7 && cnt[i+j] && cnt[i+j+1] && cnt[i+j+2]) {
                cnt[i+j]--; cnt[i+j+1]--; cnt[i+j+2]--;
                res++;
            }
        }
    }
    for(int i = 0; i < 7; ++i) {
        if(cnt[i+27] >= 3) {
            cnt[i+27] -= 3;
            res++;
        }
    }
    if(res == 4) return true;
    else return false;
}

bool check1() {
    for(int i = 0; i < 34; ++i) {
        if(a[i] >= 2) {
            a[i] -= 2;
            if(check43()) {
                a[i] += 2;
                return true;
            }
            a[i] += 2;
        }
    }
    return false;
}

bool check2() {
    for(int i = 0; i < 34; ++i) {
        if(a[i] != 2 && a[i] != 0) return false;
    }
    return true;
}

bool check3() {
    if(!a[0] || !a[8] || !a[9] || !a[17] || !a[18] || !a[26]) return false;
    for(int i = 0; i < 7; ++i)
        if(!a[i+27]) return false;
    for(int i = 0; i < 27; i += 9){
        for(int j = 1; j < 8; j++)
          if(a[i+j] != 0) return false;
    }
    return true;
}

bool check() {
    if(check1() || check2() || check3()) return true;
    return false;
}

int main() {
    scanf("%d", &kase);
    while(kase--) {
        memset(a, 0, sizeof(a));
        for(int i = 0; i < 13; ++i) {
            char s[3]; scanf("%s", s);
            int t = s[0]-'1';

            if(s[1] == 's') t += 9;
            else if(s[1] == 'p') t += 18;
            else if(s[1] == 'c') t += 27;
            a[t]++;
        }
        int k = 0;
        for(int i = 0; i < 34; ++i) {
            a[i]++;
            if(a[i] <= 4 && check()) {
                ans[k++] = i;
            }
            a[i]--;
        }
        if(!k) { printf("Nooten\n"); continue; }
        printf("%d", k);
        char ss[5] = {'m','s','p','c'};
        for(int i = 0; i < k; ++i) {
            int t1 = ans[i]/9;
            int t2 = ans[i]%9+1;
            printf(" %d%c", t2, ss[t1]);
        }
        puts("");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章