成都胡牌

    1. import java.util.ArrayList;  
    2. import java.util.List;  
    3. import java.util.Scanner;  
    4.   
    5.   
    6. public class Main {  
    7.   
    8.     public static void main(String[] args) {  
    9.         // TODO Auto-generated method stub  
    10.   
    11.         int[] count = new int[27];//TDW排列  
    12.         int[] tmp = new int[27];//TDW排列  
    13.         List<String> huPai = new ArrayList<String>();  
    14.         for(int i=0;i<27;i++){  
    15.             count[i]=0;  
    16.         }  
    17.         Scanner cin = new Scanner(System.in);  
    18.         String line = cin.nextLine();  
    19.         int len = line.length();  
    20.         for(int i=0;i<len;i += 2){  
    21.             if(line.charAt(i+1)=='T'){  
    22.                 int num = Integer.parseInt(line.charAt(i)+"")-1;  
    23.                 count[num]++;  
    24.             }else if(line.charAt(i+1)=='D'){  
    25.                 int num = Integer.parseInt(line.charAt(i)+"")-1;  
    26.                 count[num+9]++;  
    27.             }else if(line.charAt(i+1)=='W'){  
    28.                 int num = Integer.parseInt(line.charAt(i)+"")-1;  
    29.                 count[num+9*2]++;  
    30.             }  
    31.         }  
    32.           
    33.         for(int i=0;i<27;i++){  
    34.             count[i]++;  
    35.             for(int j=0;j<27;j++)  
    36.                 tmp[j] = count[j];  
    37.             boolean flag = isHU(tmp);  
    38.             if(flag)  
    39.                 huPai.add(translate(i));  
    40.             count[i]--;  
    41.         }  
    42.         System.out.println(huPai.size());  
    43.         for(String x:huPai)  
    44.             System.out.print(x);  
    45.     }  
    46.   
    47.     private static String translate(int i) {  
    48.         // TODO Auto-generated method stub  
    49.         int n = i/9;  
    50.         int pre = i%9+1;  
    51.         String r = null;  
    52.         switch (n) {  
    53.         case 0:  
    54.             r = pre + "T";  
    55.             break;  
    56.         case 1:  
    57.             r = pre + "D";  
    58.             break;  
    59.         case 2:  
    60.             r = pre + "W";  
    61.             break;  
    62.         default:  
    63.             break;  
    64.         }  
    65.         return r;  
    66.     }  
    67.   
    68.     private static boolean isHU(int[] count) {  
    69.         // TODO Auto-generated method stub  
    70.         boolean result = tryHU(count, 14);  
    71.         return result;  
    72.     }  
    73.   
    74.     private static boolean tryHU(int[] count, int len) {  
    75.         // TODO Auto-generated method stub  
    76.         if(len==0)  
    77.             return true;  
    78.         if(len%3==2){//说明对牌没出现  
    79.             for(int i=0;i<27;i++){  
    80.                 if(count[i]>=2){  
    81.                     count[i] -= 2;  
    82.                     if(tryHU(count, len - 2))  
    83.                         return true;  
    84.                     count[i] += 2;  
    85.                 }  
    86.             }  
    87.         }  
    88.         else{  
    89.             //三个一样的  
    90.             for(int i=0;i<27;i++){  
    91.                 if(count[i]>=3){  
    92.                     count[i] -= 3;  
    93.                     if(tryHU(count, len - 3))  
    94.                         return true;  
    95.                     count[i] += 3;  
    96.                 }  
    97.             }  
    98.             //是否是顺子  
    99.             for(int i=0;i<27-2;i++){  
    100.                 if(count[i]>0&&count[i+1]>0&&count[i+2]>0){  
    101.                     count[i] -= 1;  
    102.                     count[i+1] -= 1;  
    103.                     count[i+2] -= 1;  
    104.                     if(tryHU(count, len - 3))  
    105.                         return true;  
    106.                     count[i] += 1;  
    107.                     count[i+1] += 1;  
    108.                     count[i+2] += 1;  
    109.                 }  
    110.             }  
    111.         }  
    112.         return false;  
    113.     }  
    114.   
    115. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值