HDOJ4431 Mahjong


思路:模拟麻将。我之前用递归TLE了,后来修改后就WA,发现以下两种情况没有考虑到。

T1:2s 2s 3s 3s 4s 4s 2p 2p 3p 3p 4p 4p 5p

ans: 2 2p 5p

T2: 1p 1p 1s 9s 1m 9m 1c 2c 3c 4c 5c 6c 7c

ans: 1 9p

(总之感谢妈咪从小就让我认识了麻将=w=)(上次有一道国际象棋的题目我就不会做,所以生活积累很重要)


Mahjong

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


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


#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const char* mahjong[] = {
    "1m", "2m", "3m", "4m", "5m", "6m", "7m", "8m", "9m",
    "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s",
    "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p",
    "1c", "2c", "3c", "4c", "5c", "6c", "7c"
};
int num[34];
int str[13] = {0, 8, 9, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33};

int convert(char *s){
    for(int i = 0; i < 34; i++){
        if(!strcmp(mahjong[i],s)) return i;
    }
    return -1;
}

int c[34];
bool search(){
    for(int i = 0; i < 34; i++){
        num[i] = c[i];
    }

    for(int i = 0; i < 34; i++){
        if(num[i] >= 3) num[i]-=3;
        if(num[i] > 0){
            int ans = 0;
            while(1){
            if(ans>18) break;
                if(i>=ans &&i<=(ans+6) && num[i] >= 1 && num[i+1] >= num[i] &&num[i+2] >=num[i]){
                    int tmp = num[i];
                    num[i] = 0;
                    num[i+1]-=tmp;
                    num[i+2]-=tmp;

                }
                ans+=9;
            }
        }
    }

    bool flag = 0;
    for(int i = 0; i < 34; i++)
        if(num[i]>0){
            flag = 1;
        }
    if(!flag) return 1;
    return 0;
}

bool check(){
    //case2
    int sum = 0;
    for(int i = 0; i < 34; i++){
        num[i] = c[i];
        if(num[i]==2) sum++;
    }
    if(sum == 7) return 1;
    //case3
    sum = 0;
    int ans = 0;
    for(int i = 0; i < 13; i++){
        if(num[str[i]]){
            sum += num[str[i]];
            ans++;
        }
    }
    if(ans == 13 && sum == 14) return 1;

    //case1
    for(int i = 0; i < 34; i++){
        if(c[i] >= 2){
            c[i]-=2;
            if(search()){
                c[i]+=2;
                return 1;
            }
            c[i]+=2;
        }
    }
    return 0;
}

vector<int> G;
int main(){
    bool ok;
    char s[100];
    int mj[15];

    int cases;
    scanf("%d", &cases);
    while(cases--){
        G.clear();
        scanf("%s", s);
        mj[0] = convert(s);
        for(int i = 1; i < 13; i++){
            scanf("%s", s);
            mj[i] = convert(s);
            if(mj[i]==-1) continue;
        }
        ok = false;

        memset(c, 0, sizeof(c));
        for(int i = 0; i < 13; i++){
            c[mj[i]]++;
        }

        for(int i = 0; i < 34; i++){
            if(c[i] >= 4) continue;
            c[i]++;
            if(check()){
                ok = true;
                G.push_back(i);
            }
            c[i]--;
        }

        if(!ok){
            printf("Nooten\n");
            continue;
        }
        printf("%d", G.size());
        for(int i = 0; i < G.size(); i++){
            printf(" %s", mahjong[G[i]]);
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值