zoj 3669 Japanese Mahjong I

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3669

题目大意:就是给你一副牌,问你胡牌的方式有几种,并输出方式。。。。。

思路:因为一副牌的数量不多,所以可以直接枚举每一张牌,判断加上这张牌后能否胡牌。。。

code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>

using namespace std;

struct node
{
    int num;
    char kind;
}s[1000];

char str[100];
int a[4][15];
int ta[4][15];
int len,cnt;

int find(int num)
{
    int i,j;
    if(num==0) return 1;
    for(i=0;i<4;i++)       //判断三个为一组的情况
    {
        for(j=1;j<=9;j++)
        {
            if(a[i][j]<3) continue;
            a[i][j]-=3;
            if(find(num-1)) return 1;
            a[i][j]+=3;     //注意这儿的回溯
        }
    }
    for(i=0;i<3;i++)      //判断连牌的情况,并且连牌只可能前三类牌中出现
    {
        for(j=1;j<=7;j++)
        {
            if(a[i][j]&&a[i][j+1]&&a[i][j+2])
            {
                a[i][j]--;
                a[i][j+1]--;
                a[i][j+2]--;
                if(find(num-1))
                {
                    return 1;
                }
                a[i][j]++;    //回溯
                a[i][j+1]++;
                a[i][j+2]++;
            }
        }
    }
    return 0;
}

int solve()
{
    int i,j;
    for(i=0;i<4;i++)
    {
        for(j=1;j<=9;j++)
        {
            if(a[i][j]<2) continue;
            if(i==3&&j<=7)
            {
                a[i][j]-=2;
                if(find(4)) return 1;
                a[i][j]+=2;
            }
            else if(i<3&&j<=9)
            {
                a[i][j]-=2;
                if(find(4)) return 1;
                a[i][j]+=2;
            }
        }
    }
    return 0;
}

int main()
{
    int i,j,k;
    while(scanf("%s",str)!=EOF)
    {
        cnt=0;
        memset(a,0,sizeof(a));
        for(i=0;i<26;i+=2)   //记录每类牌的数量
        {
            if(str[i+1]=='m')
            {
                a[0][str[i]-'0']++;
            }
            else if(str[i+1]=='p')
            {
                a[1][str[i]-'0']++;
            }
            else if(str[i+1]=='s')
            {
                a[2][str[i]-'0']++;
            }
            else if(str[i+1]=='z')
            {
                a[3][str[i]-'0']++;
            }
        }
        char kind[5]="mpsz";
        for(i=1;i<=27+7;i++)  //枚举每张牌表并判断
        {
            int id=(i-1)/9;
            int no=i%9;
            if(no==0)
            {
                no=9;
            }
            memcpy(ta,a,sizeof(a));
            if(id<4&&a[id][no]<4)
            {
                a[id][no]++;
            }
            else
            {
                continue;
            }
            if(solve())   //如果能胡牌,则记录下来
            {
                s[cnt].num=no;
                s[cnt++].kind=kind[id];
            }
            memcpy(a,ta,sizeof(ta));
        }
        if(cnt==0)
        {
            printf("0\n");
        }
        else
        {
            printf("%d ",cnt);
            for(i=0;i<cnt;i++)
            {
                printf("%d%c",s[i].num,s[i].kind);
            }
            printf("\n");
        }
    }
    return 0;
}
时间: 2024-10-07 19:00:49

zoj 3669 Japanese Mahjong I的相关文章

ZOJ 3671 Japanese Mahjong III

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3671 Japanese Mahjong III Time Limit: 2 Seconds      Memory Limit: 65536 KB Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this proble

ZOJ3669:Japanese Mahjong I

Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this problem, we concentrate on Japanese Mahjong, a variation of mahjong. For brief, all of the word mahjong mentioned following refer to Japanese Mahjon

ZOJ3671:Japanese Mahjong III

Mahjong is a game of skill, strategy and calculation and involves a certain degree of chance. In this problem, we concentrate on Japanese Mahjong, a variation of mahjong. For brief, all of the word mahjong mentioned following refer to Japanese Mahjon

ZOJ Monthly, November 2012

A.ZOJ 3666 Alice and Bob 组合博弈,SG函数应用 #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 10000 + 100; int SG[maxn]; vector<int> g[maxn]; int mex(int u) { //minimal exc

HDU 4431 Mahjong (麻将、神坑模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4431 题面: Mahjong Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4219    Accepted Submission(s): 842 Problem Description Japanese Mahjong is a fou

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

ZOJ 3492 Kagome Kagome

 F - Kagome Kagome Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3492 Description Kagome kagome, kago no naka no tori wa Itsu itsu deyaru? Yoake no ban ni Tsuru to kame to subetta. Ushiro no s

bzoj 3669: [Noi2014]魔法森林

3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MB 动点spfa Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节点1,隐士则住在号节点N.小E需要通过这一片魔法森林,才能够拜访到隐士. 魔法森林中居住了一些妖怪.每当有人经过一条边的时候,这条边上的妖怪就会对其发起攻击.幸运的

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an