HDU 4431 Mahjong 模拟

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

不能说是水题了,具体实现还是很恶心的...几乎优化到哭但是DFS(还加了几个剪枝)还是不行...搜索一直T到死...贪心构造解就行算是长见识了

首先还是要枚举所有34张牌...然后再枚举将牌(2个的)...之后就是判断这副牌有没有胡牌了...肯定是要特判国士无双和七对的...再就是平胡的情况

平胡只要对每张牌优先刻子,再直接贪心向下构造顺子就行了......

/********************* Template ***********************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define EPS             1e-8
#define DINF            1e15
#define MAXN            100050
#define MOD             1000000007
#define INF             0x7fffffff
#define LINF            1LL<<60
#define PI              3.14159265358979323846
#define lson            l,m,rt<<1
#define rson            m+1,r,rt<<1|1
#define BUG             cout<<"BUG! "<<endl
#define ABS(a)          ((a)>0?(a):(-a))
#define LINE            cout<<"------------------ "<<endl
#define FIN             freopen("in.txt","r",stdin)
#define FOUT            freopen("out.txt","w",stdout)
#define mem(a,b)        memset(a,b,sizeof(a))
#define FOR(i,a,b)      for(int i = a ; i < b ; i++)
#define read(a)         scanf("%d",&a)
#define read2(a,b)      scanf("%d%d",&a,&b)
#define read3(a,b,c)    scanf("%d%d%d",&a,&b,&c)
#define write(a)        printf("%d\n",a)
#define write2(a,b)     printf("%d %d\n",a,b)
#define write3(a,b,c)   printf("%d %d %d\n",a,b,c)
#pragma comment         (linker,"/STACK:102400000,102400000")
template<class T> inline T L(T a)               {return (a << 1);}
template<class T> inline T R(T a)               {return (a << 1 | 1);}
template<class T> inline T lowbit(T a)          {return (a & -a);}
template<class T> inline T Mid(T a,T b)         {return ((a + b) >> 1);}
template<class T> inline T gcd(T a,T b)         {return b ? gcd(b,a%b) : a;}
template<class T> inline T lcm(T a,T b)         {return a / gcd(a,b) * b;}
template<class T> inline T Min(T a,T b)         {return a < b ? a : b;}
template<class T> inline T Max(T a,T b)         {return a > b ? a : b;}
template<class T> inline T Min(T a,T b,T c)     {return min(min(a,b),c);}
template<class T> inline T Max(T a,T b,T c)     {return max(max(a,b),c);}
template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));}
template<class T> inline T mod(T x,T y)         {y = ABS(y); return x >= 0 ? x % y : x % y + y;}
template<class T> inline T mul_mod(T a,T b,T n) {
    T ret = 0,tmp = a % n;
    while(b){
        if((b&1) && (ret+=tmp)>=n) ret -= n;
        if((b>>=1) && (tmp<<=1)>=n) tmp -= n;
    }return ret;
}
template<class T> inline T pow_mod(T a,T b,T n){
    T ret = 1; a = a % n;
    while(b){
        if (b&1) ret = mul_mod(ret,a,n);
        if (b>>=1) a = mul_mod(a,a,n);
    }return ret;
}
template<class T> inline T exGCD(T a, T b, T &x, T &y){
    if(!b) return x = 1,y = 0,a;
    T res = exGCD(b,a%b,x,y),tmp = x;
    x = y,y = tmp - (a / b) * y;
    return res;
}
template<class T> inline T reverse_bits(T x){
    x = (x >> 1 & 0x55555555) | ((x << 1) & 0xaaaaaaaa); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
    x = (x >> 4 & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
    x = (x >>16 & 0x0000ffff) | ((x <<16) & 0xffff0000); return x;
}
typedef long long LL;    typedef unsigned long long ULL;
typedef __int64 LL;      typedef unsigned __int64 ULL;
/********************   By  F   ********************/
int ma[5][10],yy[50],xx[50];
//bool dfs(int c){
//    if(c == 12) return true;
//    for(int i = 0 ; i < 4 ; i++){
//        for(int j = 1 ; j <= (i==3?7:9) ; j++){
//            if(i != 3 && j <= 7 && ma[i][j] >= 1 && ma[i][j+1] >= 1 && ma[i][j+2] >= 1){
//                ma[i][j] -= 1; ma[i][j+1] -= 1; ma[i][j+2] -= 1;
//                if(dfs(c+3)) {
//                    ma[i][j] += 1; ma[i][j+1] += 1; ma[i][j+2] += 1;
//                    return true;
//                }
//                ma[i][j] += 1; ma[i][j+1] += 1; ma[i][j+2] += 1;
//            }
//            if(ma[i][j] >= 3){
//                ma[i][j] -= 3;
//                if(dfs(c+3)) {
//                    ma[i][j] += 3;
//                    return true;
//                }
//                ma[i][j] += 3;
//            }
//        }
//    }
//    return false;
//}
bool dfs(){
    int ct = 0;
    int tmp[4][10];
    for(int i = 0 ; i < 4 ; i++)
        for(int j = 1 ; j <= 9 ; j++)
            tmp[i][j] = ma[i][j];
    for(int i = 0 ; i < 3 ; i++){
        for(int j = 1 ; j <= 9 ; j++){
            if(tmp[i][j] >= 3){
                tmp[i][j] -= 3;
                ct++;
            }
            while(j+2 <= 9 && tmp[i][j] >= 1 && tmp[i][j+1] >= 1 && tmp[i][j+2] >= 1){
                tmp[i][j]-=1;
                tmp[i][j+1]-=1;
                tmp[i][j+2]-=1;
                ct++;
            }
        }
    }
    for(int i = 1 ; i <= 9 ; i++)
        if(tmp[3][i] >= 3) ct++;
    if(ct == 4) return true;
    else return false;
}
bool judgeGuo(){
    int ct1 = 0,ct2 = 0;
    for(int i = 0 ; i < 3 ; i++){
        if(ma[i][1] == 1) ct1++;
        if(ma[i][1] == 2) ct2++;
        if(ma[i][9] == 1) ct1++;
        if(ma[i][9] == 2) ct2++;
        if(ct2 > 1) return false;
    }
    for(int i = 1 ; i <= 7 ; i++){
        if(ma[3][i] == 1) ct1++;
        if(ma[3][i] == 2) ct2++;
        if(ct2 > 1) return false;
    }
    if(ct1 == 12 && ct2 == 1) return true;
    return false;
}
bool judge7dui(){
    int cnt = 0;
    for(int i = 0 ; i < 4 ; i++){
        for(int j = 1 ; j <= (i==3?7:9) ; j++){
            if(ma[i][j] != 2 && ma[i][j] != 0) return false;
            if(ma[i][j] == 2) cnt++;
        }
    }
    if(cnt == 7) return true;
    return false;
}
bool judge(){
    if(judgeGuo()) return true;
    if(judge7dui()) return true;
    for(int i = 0 ; i < 4 ; i++){
        for(int j = 1 ; j <= (i==3?7:9) ; j++){
            if(ma[i][j] >= 2){
                ma[i][j] -= 2;
                if(dfs()) {
                    ma[i][j] += 2;
                    return true;
                }
                ma[i][j] += 2;
            }
        }
    }
    return false;
}
int main(){
    //FIN;
    //FOUT;
    int T;
    char t[4];
    scanf("%d",&T);
    int i,j;
    while(T--){
        mem(ma,0);
        for(i = 0 ; i < 13 ; i++){
            scanf("%s",t);
            if(t[1] == 'm') ma[0][t[0]-'0']++;
            else if(t[1] == 's') ma[1][t[0]-'0']++;
            else if(t[1] == 'p') ma[2][t[0]-'0']++;
            else if(t[1] == 'c') ma[3][t[0]-'0']++;
        }
        int cnt = 0;
        for(i = 0 ; i < 4 ; i++){
            for(j = 1 ; j <= (i==3?7:9) ; j++){
                if(ma[i][j] < (i==3?3:4) ){
                    ma[i][j]++;
                    if(judge()) {
                        xx[cnt] = i;
                        yy[cnt++] = j;
                    }
                    ma[i][j]--;
                }
            }
        }
        if(cnt == 0){
            printf("Nooten\n");
            continue;
        }else{
            printf("%d",cnt);
            for(i = 0 ; i < cnt ; i++){
                if(xx[i] == 0) printf(" %dm",yy[i]);
                else if(xx[i] == 1) printf(" %ds",yy[i]);
                else if(xx[i] == 2) printf(" %dp",yy[i]);
                else if(xx[i] == 3) printf(" %dc",yy[i]);
            }
            putchar('\n');
        }
    }
    return 0;
}

 

posted @ 2013-11-23 20:36  Felix_F  阅读(221)  评论(0编辑  收藏  举报