UVA 11210 - Chinese Mahjong

来源:互联网 发布:iptv网络电视频道 编辑:程序博客网 时间:2024/05/18 07:18

UVA 11210 - 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 FA1S 2S 3S 4S 5S 6S 7S 8S 9S 1T 3T 5T 7T0

Output for the Sample Input

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

 

思路分析

一个关于打麻将的题目,花了较长的时间才弄懂了这个题,不过今年过年回去肯定可以赢爷爷和外公的钱了^-^

题目的大致意思是:

你现在手上有13张牌,当对方出一张牌时看看你能不能胡牌,等价于你现在要从手中的13张牌中找出要“听”的那一张;

题目中给出胡牌的条件是当有两个相同的牌时(将牌),剩下的12张牌(假设又抓进了一张牌)中都是对子和/或顺子。

因此,需要模拟你胡牌的所有情况,即假设你胡的是34种牌的其中一张(从第一种到最后一种),然后依次进行判断(暴力法),判断的时候首先确定将牌,然后对剩下的牌判断是否全为对子和/或顺子,该过程用到了深搜,若深搜到底不成立时,则回溯进行下一张牌的判断。

代码如下:

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cstring>

#include <string>

#include <map>

#include <set>

#include <vector>

#include <list>

#include <algorithm>

using namespace std;

const int N = 34;

const char majong[N][N] = {

"1T","2T","3T","4T","5T","6T","7T","8T","9T",

"1S","2S","3S","4S","5S","6S","7S","8S","9S",

"1W","2W","3W","4W","5W","6W","7W","8W","9W",

"DONG","NAN","XI","BEI","ZHONG","FA","BAI"

};

int ma[N];

int init(char *s)

{

    for (int i=0;i<34;i++)

        if (strcmp(s,majong[i]) == 0)

            return i;

}//返回该牌在枚举数组中的位置

bool dfs(int t)

{

    for (int i=0;i<34;i++)

    {

        if (ma[i] >= 3)

        {

            ma[i]-=3;//假设把该牌视为对子

            if (t == 3)

                return true;//t=3即表示有四对对子或顺子(从0到3)

            if (dfs(t+1))

                return true;//判断把ma[i]视为对子时剩余的牌是否能够凑成4-t对对子或顺子

            ma[i]+=3;//若将ma[i]视为对子时剩余的牌不能胡时取消假设

        }

 

    }

    for (int i=0;i<24;i++)//只有到majong数组中的第25个(即7w)之间的牌才能“起”顺子

    {

        if (i%9 <= 6 && ma[i] >= 1 && ma[i+1] >= 1 && ma[i+2] >= 1)//i%9<=6表示只有“桶”、“条”、“万”的前七个才能“起”顺子

        {

            ma[i]--;

            ma[i+1]--;

            ma[i+2]--;//假设把这三副连续的牌视为顺子

            if (t == 3)

                return true;

            if (dfs(t+1))

                return true;

            ma[i]++;

            ma[i+1]++;

            ma[i+2]++;//若将以ma[i]“起”顺子时剩余的牌不能胡时取消假设

 

        }

    }

    return false;

}

bool check()

{

    for (int i=0;i<34;i++)

    {

        if (ma[i] >= 2)

        {

            ma[i]-=2;//假设把该牌视为将牌

            if (dfs(0))

                return true;//判断ma[i]为将牌时剩余的12张牌能否凑成4对对子或顺子

            ma[i]+=2;//若不能胡牌则取消假设

        }

 

 

    }

    return false;

}

int main()

{

    int Case=1;

    int tmp[N];

    char ch[N];

    bool mark;

    while (scanf("%s",ch) != EOF)

    {

        if (ch[0] == '0')

            break;

        tmp[0]=init(ch);

        for (int i=1;i<13;i++)

        {

            scanf("%s",ch);

            tmp[i]=init(ch);

        }     //对每张牌进行规范编号

        mark=false;

        printf("Case %d:",Case++);

        for (int i=0;i<34;i++)

        {

            memset(ma,0,sizeof(ma));

            for (int j=0;j<13;j++)

                ma[tmp[j]]++;//记录每种牌的个数

            if (ma[i] == 4)

                continue;//每种牌只有四个,即不可能“听”该牌

            ma[i]++;//假设“听”的就是这张牌

            if (check())

            {

                mark=true;

                printf(" %s",majong[i]);

            }

            ma[i]--;//若进入该张牌不能胡牌时取消假设

 

        }

        if (mark == false)

            printf(" Not ready");

        printf("\n");

    }

    return 0;

}

 

0 0
原创粉丝点击