本文主要是介绍hdu4431麻将模拟,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
给13张牌。问增加哪些牌可以胡牌。
胡牌有以下几种情况:
1、一个对子 + 4组 3个相同的牌或者顺子。
2、7个不同的对子。
3、13幺
贪心的思想:
对于某张牌>=3个,先减去3个相同,再组合顺子。
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;public class Main {public static void main(String[] args) throws IOException{StreamTokenizer cin = new StreamTokenizer(new BufferedInputStream(System.in)); InputReader in = new InputReader(System.in) ;PrintWriter out = new PrintWriter(System.out) ;int t = in.nextInt() ;while(t-- > 0){new Task().solve(in, out) ; //out.flush() ;}out.flush() ;}}class Task{static ArrayList<String> majiang = new ArrayList<String>(Arrays.asList("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" ));static int[] num = new int[34] ;static ArrayList<Integer> ad = new ArrayList<Integer>(Arrays.asList(0,8,9,17,18,26)) ; boolean helper(){if(yes1()) return true ;if(yes2()) return true ;for(int i = 0 ; i < 34 ; i++){if(num[i] >= 2){num[i] -= 2 ;if(yes3()){num[i] += 2 ;return true ;}num[i] += 2 ;}}return false ;}boolean yes1(){int g = 0 ; for(int i = 0 ; i < 34 ; i++){if(num[i] == 2) g++ ;}return g == 7 ;}boolean yes2(){int x = 0 , y = 0 ; for(int i : ad){if(num[i] == 1) x++ ;else if(num[i] == 2) y++ ;}for(int i = 27 ; i < 34 ; i++){if(num[i] == 1) x++ ;else if(num[i] == 2) y++ ;}return x == 12 && y == 1 ;}boolean yes3(){int[] a = new int[34] ;//System.arraycopy(num, 0 , a, 0, 34) ;for(int i = 0 ; i < 34 ; i++) a[i] = num[i] ;int sum = 0 ;for(int i = 0 ; i <= 18 ; i += 9){for(int j = 0 ; j < 9 ; j++){int k = i + j ;if(a[k] >= 3){a[k] -= 3 ;sum++ ;}while((j <= 6) && (a[k] > 0) && (a[k+1] > 0) && (a[k+2] > 0)){a[k]-- ;a[k+1]-- ;a[k+2]-- ;sum++ ;}}}for(int i = 27 ; i < 34 ; i++){if(a[i] >= 3){a[i] -= 3 ;sum++ ;}}return sum == 4 ;}public void solve(InputReader in , PrintWriter out) throws IOException{ArrayList<String> ans = new ArrayList<String>() ;Arrays.fill(num, 0) ;for(int i = 0 ; i <= 12 ; i++) num[majiang.indexOf(in.next())]++ ;for(int i = 0 ; i < 34 ; i++){num[i]++ ; if(num[i] <= 4 && helper()) ans.add(majiang.get(i)) ;num[i]-- ; }if(ans.size() == 0) out.println("Nooten") ;else{out.print(ans.size()) ;for(String e : ans) out.print(" " + e) ;out.println() ;}}}class InputReader{public BufferedReader reader;public StringTokenizer tokenizer;public InputReader(InputStream stream){reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = null;}public String next(){while(tokenizer == null || !tokenizer.hasMoreTokens()){try{tokenizer = new StringTokenizer(reader.readLine());}catch (IOException e) {throw new RuntimeException(e);}}return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}}
这篇关于hdu4431麻将模拟的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!