zoukankan      html  css  js  c++  java
  • HDU-4431 麻将

    Japanese Mahjong is a four-player game. The game needs four people to sit around a desk and play with a set of Mahjong tiles. A set of Mahjong tiles contains four copies of the tiles described next: 

    One to nine Man, which we use 1m to 9m to represent; 

    One to nine Sou, which we use 1s to 9s to represent; 

    One to nine Pin, which we use 1p to 9p to represent; 

    Character tiles, which are:Ton, Nan, Sei, Pei, Haku, Hatsu, Chun, which we use 1c to 7c to represent. 

    A winning state means a set of 14 tiles that normally contains a pair of same tiles (which we call "eyes") and four melds. A meld is formed by either three same tiles(1m, 1m, 1m or 2c, 2c, 2c for example) or three continuous non-character tiles(1m, 2m, 3m or 5s, 6s, 7s for example). 

    However, there are two special winning states that are different with the description above, which are: 
    "Chii Toitsu", which means 7 different pairs of tiles; 
    "Kokushi Muso", which means a set of tiles that contains all these tiles: 1m, 9m, 1p, 9p, 1s, 9s and all 7 character tiles. And the rest tile should also be one of the 13 tiles above. 

    And the game starts with four players receiving 13 tiles. In each round every player must draw one tile from the deck one by one. If he reaches a winning state with these 14 tiles, he can say "Tsu Mo" and win the game. Otherwise he should discard one of his 14 tiles. And if the tile he throws out can form a winning state with the 13 tiles of any other player, the player can say "Ron" and win the game. 

    Now the question is, given the 13 tiles you have, does there exist any tiles that can form a winning state with your tiles? 

    (Notes: Some of the pictures and descriptions above come from Wikipedia.) 

    InputThe input data begins with a integer T(1≤T≤20000). Next are T cases, each of which contains 13 tiles. The description of every tile is as above.OutputFor each cases, if there actually exists some tiles that can form a winning state with the 13 tiles given, print the number first and then print all those tiles in order as the description order of tiles above. Otherwise print a line "Nooten"(without quotation marks).Sample Input

    2
    1s 2s 3s 2c 2c 2c 2p 3p 5m 6m 7m 1p 1p
    1p 1p 2p 3p 4s 5s 6s 7c 7c 3s 3s 2m 2m

    Sample Output

    2 1p 4p
    Nooten

    思路:枚举第14张牌,第二种情况和第三种情况好判断,讨论到第一种情况枚举那个“对”,及时return 剪枝,计算量在O(T*34 * 6 * 34)左右。
    废话:大模拟一发过,美滋滋
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <set>
    #include <map>
    #include <list>
    #include <iomanip>
    #include <cctype>
    #include <cassert>
    #include <bitset>
    #include <ctime>
    
    using namespace std;
    
    #define pau system("pause")
    #define ll long long
    #define pii pair<int, int>
    #define pb push_back
    #define mp make_pair
    #define clr(a, x) memset(a, x, sizeof(a))
    
    const double pi = acos(-1.0);
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    const double EPS = 1e-9;
    
    int T;
    int A[4][10], B[4][10];
    bool ok_1() {
        memcpy(B, A, sizeof(A));
        /*for (int i = 0; i <= 3; ++i) {
            for (int j = 1; j <= 9; ++j) {
                printf("%d ", B[i][j]);
            }
            puts("");
        }pau;*/
        int cnt_t = 0;
        for (int i = 0; i < 3; ++i) {
            for (int j = 1; j <= 7; ++j) {
                if (B[i][j] > 2) {
                    B[i][j] -= 3;
                    ++cnt_t;
                }
                if (B[i][j]) {
                    if (B[i][j + 1] < B[i][j] || B[i][j + 2] < B[i][j]) {
                        return false;
                    }
                    B[i][j + 1] -= B[i][j];
                    B[i][j + 2] -= B[i][j];
                    cnt_t += B[i][j];
                    B[i][j] = 0;
                }
            }
            for (int j = 8; j <= 9; ++j) {
                if (B[i][j] == 3) {
                    ++cnt_t;
                } else if (B[i][j]) {
                    return false;
                }
            }
        }
        for (int j = 1; j <= 7; ++j) {
            if (3 == B[3][j]) {
                ++cnt_t;
            } else if (B[3][j]) {
                return false;
            }
        }
        //cout << cnt_t << endl; pau;
        return 4 == cnt_t;
    }
    bool is_1() {
        for (int i = 0; i < 3; ++i) {
            for (int j = 1; j <= 9; ++j) {
                if (A[i][j] > 1) {
                    A[i][j] -= 2;
                    if (ok_1()) {
                        A[i][j] += 2;
                        return true;
                    }
                    A[i][j] += 2;
                }
            }
        }
        for (int j = 1; j <= 7; ++j) {
            if (A[3][j] > 1) {
                A[3][j] -= 2;
                if (ok_1()) {
                    A[3][j] += 2;
                    return true;
                }
                A[3][j] += 2;
            }
        }
        return false;
    }
    bool is_2() {
        int cnt_p = 0;
        for (int i = 0; i < 4; ++i) {
            for (int j = 1; j <= 9; ++j) {
                cnt_p += A[i][j] > 1;
            }
        }
        return 7 == cnt_p;
    }
    bool is_3() {
        for (int i = 0; i < 3; ++i) {
            if (!A[i][1] || !A[i][9]) {
                return false;
            }
            for (int j = 2; j <= 8; ++j) {
                if (A[i][j]) {
                    return false;
                }
            }
        }
        for (int j = 1; j <= 7; ++j) {
            if (!A[3][j]) {
                return false;
            }
        }
        return true;
    }
    int main() {
        scanf("%d", &T);
        while (T--) {
            clr(A, 0);
            vector<pii> ans;
            for (int i = 1; i <= 13; ++i) {
                int x;
                char c;
                scanf("%d%c", &x, &c);
                if ('m' == c) {
                    ++A[0][x];
                } else if ('s' == c) {
                    ++A[1][x];
                } else if ('p' == c) {
                    ++A[2][x];
                } else {
                    ++A[3][x];
                }
            }
            for (int i = 0; i < 4; ++i) {
                for (int j = 1; j <= 9; ++j) {
                    if (A[i][j] < 4) {
                        ++A[i][j];
                        if (is_3() || is_2() || is_1()) {
                            ans.pb(mp(j, i));
                        }
                        --A[i][j];
                    }
                    if (3 == i && 7 == j) {
                        break;
                    }
                }
            }
            if (ans.size()) {
                printf("%d", ans.size());
                for (int i = 0; i < ans.size(); ++i) {
                    int x = ans[i].first, y = ans[i].second;
                    printf(" %d", x);
                    if (!y) {
                        putchar('m');
                    } else if (1 == y) {
                        putchar('s');
                    } else if (2 == y) {
                        putchar('p');
                    } else {
                        putchar('c');
                    }
                }
                puts("");
            } else {
                puts("Nooten");
            }
        }
        return 0;
    }
  • 相关阅读:
    移动H5页面微信支付踩坑之旅(微信支付、单页面路由模拟、按钮加锁、轮询等常见功能)
    EDM模板编写踩坑指南(持续更新中)
    7天内我面试了10家公司,如何从命中率0%到命中率至70%?
    codewars.DNA题目几种解法分析(字符串替换)
    webpack+react搭建环境
    手机端访问网页 js根据浏览器区别转入手机端网址的URL
    树状数组————(神奇的区间操作)蒟蒻都可以看懂,因为博主就是个蒟蒻
    DFS————从普及到IOI(暴力骗分小能手)
    Manacher(马拉车)————O(n)回文子串
    离散化————实现梦想的算法
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/8447192.html
Copyright © 2011-2022 走看看