HDU 4431 Mahjong(枚举,模拟)

转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 

题目;给出麻将的13张牌,问再抓一张什么牌,可以胡~~~

http://acm.hdu.edu.cn/showproblem.php?pid=4431 

表示麻将一点都不会,简单看了下题,然后听victoria说了下,就开始搞了。

枚举所有的牌,然后判断14张牌有没有胡

首先是判断一下13么和7小对,这个比较好判断

其中7小对victoria的规则和题目说的有点不一样,导致WA了一次,题目要求7小对是不同的,也就是有4张牌一样不算两个对

然后是普通的胡法,感觉非常不好写,由于 可以是顺 子或者3个的,所以只能搜索了

结果还T了,自己太随意+太紧了。

整体的编码难度不大,思路也比较清晰

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<cmath>
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
map<string,int>m;
void Init()
{
    m["1m"]=1;m["2m"]=2;m["3m"]=3;
    m["4m"]=4;m["5m"]=5;m["6m"]=6;
    m["7m"]=7;m["8m"]=8;m["9m"]=9;
    m["1s"]=10;m["2s"]=11;m["3s"]=12;
    m["4s"]=13;m["5s"]=14;m["6s"]=15;
    m["7s"]=16;m["8s"]=17;m["9s"]=18;
    m["1p"]=19;m["2p"]=20;m["3p"]=21;
    m["4p"]=22;m["5p"]=23;m["6p"]=24;
    m["7p"]=25;m["8p"]=26;m["9p"]=27;
    m["1c"]=28;m["2c"]=29;m["3c"]=30;
    m["4c"]=31;m["5c"]=32;m["6c"]=33;
    m["7c"]=34;
}
int c[35];int b[14];
int c13[13]={1,9,10,18,19,27,28,29,30,31,32,33,34};
//13幺
bool judge1()
{
    int c1=0,c2=0;
    for(int i=0;i<13;i++)
    {
        if(c[c13[i]]) c1++;
        if(c[c13[i]]>1) c2++;
    }
    if(c1==13&&c2==1) return true;
    return false;
}
//7小对
bool judge2()
{
    for(int i=0;i<14;i+=2)
    {

        if((i&&b[i]==b[i-1])||b[i]!=b[i+1]) return false;
    }
    return true;
}
bool flag;
bool check(int idx)
{
    if(idx>=1&&idx<=7) return true;
    if(idx>=10&&idx<=16) return true;
    if(idx>=19&&idx<=25) return true;
    return false;
}
void dfs(int idx,int k1,int k2)
{
    if(flag) return;
    if(k1>4||k2>1) return ;
    if(idx==14)
    {
        if(k1==4&&k2==1){flag=true;}
        return ;
    }
    for(int i=1;i<=34;i++)
    {
        int mark=0;
        if(c[i]>=3) {c[i]-=3;mark++;dfs(idx+3,k1+1,k2);c[i]+=3;}
        if(c[i]>=2) {c[i]-=2;mark++;dfs(idx+2,k1,k2+1);c[i]+=2;}
        if(check(i)&&c[i]&&c[i+1]&&c[i+2]) {c[i]--;c[i+1]--;c[i+2]--;mark++;dfs(idx+3,k1+1,k2);c[i]++;c[i+1]++;c[i+2]++;}
        if(c[i]&&mark==0) return ;
        if(c[i]) break;
    }
}
int a[14];
int slove()
{

    memcpy(b,a,sizeof(a));
    sort(b,b+14);
    mem(c,0);
    for(int i=0;i<14;i++)
    {
        c[b[i]]++;
        if(c[b[i]]>4)return false;
    }
    if(judge1()) return true;
    if(judge2()) return true;
    flag=false;
    dfs(0,0,0);
    return flag;
}

int main()
{
    m.clear();
    Init();
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=0;i<13;i++)
        {
            string s;
            cin>>s;
            a[i]=m[s];
        }
        int ans[50],cnt=0;
        for(int i=1;i<=34;i++)
        {
            a[13]=i;
            if(slove()) ans[cnt++]=i;
        }
        if(cnt==0) cout<<"Nooten"<<endl;
        else
        {
            cout<<cnt;
            for(int i=0;i<cnt;i++)
            {
                for(map<string,int>::iterator it=m.begin();it!=m.end();it++)
                {
                    if(it->second==ans[i])
                    {
                        cout<<" "<<(it->first);
                        break;
                    }
                }
            }
            cout<<endl;
        }
    }
    return 0;
}


展开阅读全文

Mahjong

09-24

Problem DescriptionnThe game of Mahjong originated in China and has become popular around the world. You do not need to have prior experience with Mahjong to solve this problem, and we will use different rules.nnIn our version of Mahjong, the player is given a set of 4K0 tiles. Each tile has an integer rank written on it, and there are four identical copies of each rank from 1 to K. For example, for K=5, the set of tiles would be: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5.nnThe player's goal is to select M tiles from this set to form a winning hand. A winning hand consists of some number (possibly zero) of triples plus exactly one pair. A pair must consist of two tiles of the same rank. A triple can be either three tiles of the same rank (e.g., "2 2 2"), or three tiles with consecutive ranks (e.g., "3 4 5"). The ranks do not wrap around -- for example, "4 5 1" is not a valid triple.nnGiven K and M, how many different winning hands are there? Two winning hands are considered the same if they use the same set of tiles, regardless of how those tiles are grouped to make triples and the pair. For instance, for K=4, M=8, the following two hands are considered the same:nn"1 2 3, 1 2 3, 4 4"nn"1 1, 2 3 4, 2 3 4"n nnInputnThe first line of the input gives the number of test cases, T(1≤T≤100). T lines follow. Each line contains two space-separated integers, K(1≤K≤200) and M(2≤M≤min(200,4K) and M≡2(mod3)).n nnOutputnFor each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of winning hands, modulo 1000000007(109+7).n nnSample Inputn4n1 2n3 5n4 5n9 14n nnSample OutputnCase #1: 1nCase #2: 9nCase #3: 20nCase #4: 13259 问答

没有更多推荐了,返回首页