24h購物| | PChome| 登入
2014-02-17 17:19:40| 人氣2,030| 回應0 | 上一篇 | 下一篇

[UVA] 11210 - Chinese Mahjong

推薦 0 收藏 0 轉貼0 訂閱站台

Problem C

Chinese Mahjong

Mahjong () is a game of Chinese origin usually played by four persons with tiles resembling dominoes and bearing various designs, which are drawn and discarded until one player wins with a hand of four combinations of three tiles each and a pair of matching tiles.

A set of Mahjong tiles will usually differ from place to place. It usually has at least 136 tiles, most commonly 144, although sets originating from America or Japan will have more. The 136-tile Mahjong includes:

Dots: named as each tile consists of a number of circles. Each circle is said to represent copper (tong) coins with a square hole in the middle. In this problem, they're represented by 1T, 2T, 3T, 4T, 5T, 6T, 7T, 8T and 9T.

Bams: named as each tile (except the 1 Bamboo) consists of a number of bamboo sticks. Each stick is said to represent a string (suo) that holds a hundred coins. In this problem, they're represented by 1S, 2S, 3S, 4S, 5S, 6S, 7S, 8S and 9S.

Craks: named as each tile represents ten thousand (wan) coins, or one hundred strings of one hundred coins. In this problem, they're represented by 1W, 2W, 3W, 4W, 5W, 6W, 7W, 8W and 9W.

Wind tiles: East, South, West, and North. In this problem, they're represented by DONG, NAN, XI, BEI.

Dragon tiles: red, green, and white. The term dragon tile is a western convention introduced by Joseph Park Babcock in his 1920 book introducing Mahjong to America. Originally, these tiles are said to have something to do with the Chinese Imperial Examination. The red tile means you pass the examination and thus will be appointed a government official. The green tile means, consequently you will become financially well off. The white tile (a clean board) means since you are now doing well you should act like a good, incorrupt official. In this problem, they're represented by ZHONG, FA, BAI.

There are 9*3+4+3=34 kinds, with exactly 4 tiles of each kind, so there are 136 tiles in total.

To who may be interested, the 144-tile Mahjong also includes:

Flower tiles:

typically optional components to a set of mahjong tiles, often contain artwork on their tiles. There are exactly one tile of each kind, so 136+8=144 tiles in total. In this problem, we don��t consider these tiles.

Chinese Mahjong is very complicated. However, we only need to know very few of the rules in order to solve this problem. A meld is a certain set of tiles in one's hand. There are three kinds of melds you need to know (to who knows Mahjong already, kong is not considered):

Pong: A set of three identical titles. Example: ;.

Chow: A set of three suited tiles in sequence. All three tiles must be of the same suites. Sequences of higher length are not permissible (unless it forms more than one meld). Obviously, wind tiles and dragon tiles can never be involved in chows. Example:;.

Eye: The pair, while not a meld, is the final component to the standard hand. It consists of any two identical tiles.

A player wins the round by creating a standard mahjong hand. That means, the hand consists of an eye and several (possible zero) pongs and chows. Note that each title can be involved in exactly one eye/pong/chow.

When a hand is one tile short of wining, the hand is said to be a ready hand, or more figuratively, 'on the pot'. The player holding a ready hand is said to be waiting for certain tiles. For example

is waiting for , and .

To who knows more about Mahjong: don't consider special winning hands such as ''.

Input

The input consists of at most 50 test cases. Each case consists of 13 tiles in a single line. The hand is legal (e.g. no invalid tiles, exactly 13 tiles). The last case is followed by a single zero, which should not be processed.

Output

For each test case, print the case number and a list of waiting tiles sorted in the order appeared in the problem description (1T~9T, 1S~9S, 1W~9W, DONG, NAN, XI, BEI, ZHONG, FA, BAI). Each waiting tile should be appeared exactly once. If the hand is not ready, print a message 'Not ready' without quotes.

Sample Input

1S 1S 2S 2S 2S 3S 3S 3S 7S 8S 9S FA FA
1S 2S 3S 4S 5S 6S 7S 8S 9S 1T 3T 5T 7T
0

Output for the Sample Input

Case 1: 1S 4S FA
Case 2: Not ready

Rujia Liu's Present 2: A Big Contest of Brute Force

查看聽牌可能,中文翻譯請參照 zerojudge

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <map>
using namespace std;
int S[10] = {}, T[10] = {}, W[10] = {};
map<string, int> Tile;
int check(int a, int b) {
    if(a == 4 && b == 1)
        return 1;
    int f;
    if(a < 4) {
        for(int i = 1; i <= 7; i++) {
            if(S[i] && S[i+1] && S[i+2]) {
                S[i]--, S[i+1]--, S[i+2]--;
                f = check(a+1, b);
                S[i]++, S[i+1]++, S[i+2]++;
                if(f)    return 1;
            }
            if(T[i] && T[i+1] && T[i+2]) {
                T[i]--, T[i+1]--, T[i+2]--;
                f = check(a+1, b);
                T[i]++, T[i+1]++, T[i+2]++;
                if(f)    return 1;
            }
            if(W[i] && W[i+1] && W[i+2]) {
                W[i]--, W[i+1]--, W[i+2]--;
                f = check(a+1, b);
                W[i]++, W[i+1]++, W[i+2]++;
                if(f)    return 1;
            }
        }       
        for(int i = 1; i <= 9; i++) {
            if(S[i] >= 3) {
                S[i] -= 3;
                f = check(a+1, b);
                S[i] += 3;
                if(f)    return 1;
            }
            if(T[i] >= 3) {
                T[i] -= 3;
                f = check(a+1, b);
                T[i] += 3;
                if(f)    return 1;
            }
            if(W[i] >= 3) {
                W[i] -= 3;
                f = check(a+1, b);
                W[i] += 3;
                if(f)    return 1;
            }
        }
        for(map<string, int>::iterator it = Tile.begin();
            it != Tile.end(); it++) {
            if(it->second >= 3) {
                it->second -= 3;
                f = check(a+1, b);
                it->second += 3;
                if(f)    return 1;
            }
        }
    } else if(b == 0)  {
        for(int i = 1; i <= 9; i++) {
            if(S[i] >= 2) {
                S[i] -= 2;
                f = check(a, b+1);
                S[i] += 2;
                if(f)    return 1;
            }
            if(T[i] >= 2) {
                T[i] -= 2;
                f = check(a, b+1);
                T[i] += 2;
                if(f)    return 1;
            }
            if(W[i] >= 2) {
                W[i] -= 2;
                f = check(a, b+1);
                W[i] += 2;
                if(f)    return 1;
            }
        }
        for(map<string, int>::iterator it = Tile.begin();
            it != Tile.end(); it++) {
            if(it->second >= 2) {
                it->second -= 2;
                f = check(a, b+1);
                it->second += 2;
                if(f)    return 1;
            }
        }
    }
    return 0;
}
int main() {
    int cases = 0;
    char ff[7][10] = {"DONG", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI"};
    while(true) {
        int i, j, k;
        char card[105];
        int n;
        memset(S, 0, sizeof(S));
        memset(T, 0, sizeof(T));
        memset(W, 0, sizeof(W));
        Tile.clear();
        for(i = 0; i < 13; i++) {
            if(scanf("%s", card) != 1)
                return 0;
            if(card[1] == 'S' && sscanf(card, "%dS", &n) == 1) {
                S[n]++;
            } else if(card[1] == 'T' && sscanf(card, "%dT", &n) == 1) {
                T[n]++;
            } else if(card[1] == 'W' && sscanf(card, "%dW", &n) == 1) {
                W[n]++;
            } else
                Tile[card]++;
        }
        int ready = 0;
        printf("Case %d:", ++cases);
        for(i = 1; i <= 9; i++) {
            if(T[i] < 4) {
                T[i]++;
                if(check(0, 0))
                    printf(" %dT", i), ready = 1;
                T[i]--;
            }
        }   
        for(i = 1; i <= 9; i++) {
            if(S[i] < 4) {
                S[i]++;
                if(check(0, 0))
                    printf(" %dS", i), ready = 1;
                S[i]--;
            }
        }
        for(i = 1; i <= 9; i++) {
            if(W[i] < 4) {
                W[i]++;
                if(check(0, 0))
                    printf(" %dW", i), ready = 1;
                W[i]--;
            }
        }
        for(i = 0; i < 7; i++) {
            int &x = Tile[ff[i]];
            if(x < 4) {
                x++;
                if(check(0, 0))
                    printf(" %s", ff[i]), ready = 1;
                x--;
            }
        }
        if(!ready)
            printf(" Not ready");
        puts("");
    }
    return 0;
}

台長: Morris
人氣(2,030) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][假解] 11898 - Killer Problem
此分類上一篇:[UVA][博弈] 11818 - Mouse & a Cheese

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文