HDU 4431 Mahjong 第37屆ACM/ICPC 天津賽區現場賽A題 (枚舉,判斷麻將胡牌)


Mahjong

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 648    Accepted Submission(s): 111


Problem Description
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.)
 

 

Input
The 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.
 

 

Output
For 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
 

 

Source
 

 

Recommend
zhoujiaqi2010
 
 
很有意思的題目。
就是給了13張牌。問增加哪些牌可以胡牌。
 
胡牌有以下幾種情況:
1、一個對子 +  4組 3個相同的牌或者順子。  只有m、s、p是可以構成順子的。東西南北這樣的牌沒有順子。
2、7個不同的對子。
3、1m,9m,1p,9p,1s,9s,1c,2c,3c,4c,5c,6c,7c.  這13種牌每種都有,而且僅有這13種牌。肯定是有一種2張。其他的1張。
 
 
首先是枚舉18+7=34張牌,加進去構成14張牌,判斷胡牌。
胡牌判斷如下。
對於第一種情況:枚舉每一個對子。然后按照順序找3張相同或者順子。如果有三種相同的,構成3張相同的。沒有就看能不能和后面的構成順子。一定要按照順序從小到大找過去。 1c```7c只能構成3張一樣的。然后判斷是不是剛好找到4組。
對於第二種情況:就是要每一種牌的數量要么是0,要么是2,這樣一定是7個不同的對子了。
對於第三種情況:就是要讓這13種牌的數量不等於0,而且其他牌的數量為0;
 
 
這種做法是Fox 提示我之后才想到的,果然不錯。
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

int cnt[35];

bool judge4X3()
{
    int ret=0;
    int tmp[35];
    for(int i=0;i<34;i++)tmp[i]=cnt[i];

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

bool judge1()
{
    for(int i=0;i<34;i++)
    {
        if(cnt[i]>=2)
        {
            cnt[i]-=2;//枚舉對子
            if(judge4X3())
            {
                cnt[i]+=2;
                return true;
            }
            cnt[i]+=2;
        }
    }
    return false;
}


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

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

bool judge()
{
    if(judge1() || judge2() || judge3())return true;
    return false;
}


int main()
{
    int T;
    char str[10];
    scanf("%d",&T);
    int ans[35],tol;
    while(T--)
    {
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<13;i++)
        {
            scanf("%s",&str);
            int t=str[0]-'1';
            if(str[1]=='m')t+=0;
            else if(str[1]=='s')t+=9;
            else if(str[1]=='p')t+=18;
            else t+=27;
            cnt[t]++;
        }
        tol=0;
        for(int i=0;i<34;i++)
        {
            cnt[i]++;
            if(cnt[i]<=4 && judge())
               ans[tol++]=i;
            cnt[i]--;
        }
        if(tol==0)printf("Nooten\n");
        else
        {
            printf("%d",tol);
            for(int i=0;i<tol;i++)
            {
                printf(" %d",(ans[i]%9)+1);
                if(ans[i]/9==0)printf("m");
                else if(ans[i]/9==1)printf("s");
                else if(ans[i]/9==2)printf("p");
                else printf("c");
            }
            printf("\n");
        }
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM