一、题目
小包最近迷上了一款叫做雀魂的麻将游戏,但是这个游戏规则太复杂,小包玩了几个月了还是输多赢少。于是生气的小包根据游戏简化了一下规则发明了一种新的麻将,只留下一种花色,并且去除了一些特殊和牌方式(例如七对子等),具体的规则如下:
总共有36张牌,每张牌是1~9。每个数字4张牌。你手里有其中的14张牌,如果这14张牌满足如下条件,即算作和牌;14张牌中有2张相同数字的牌,称为雀头。除去上述2张牌,剩下12张牌可以组成4个顺子或刻子。顺子的意思是递增的连续3个数字牌(例如234,567等),刻子的意思是相同数字的3个数字牌(例如111,777)
例如:
1 1 1 2 2 2 6 6 6 7 7 7 9 9 可以组成1,2,6,7的4个刻子和9的雀头,可以和牌
1 1 1 1 2 2 3 3 5 6 7 7 8 9 用1做雀头,组123,123,567,789的四个顺子,可以和牌
1 1 1 2 2 2 3 3 3 5 6 7 7 9 无论用1 2 3 7哪个做雀头,都无法组成和牌的条件。
现在,小包从36张牌中抽取了13张牌,他想知道在剩下的23张牌中,再取一张牌,取到哪几种数字牌可以和牌。
二、代码
import java.util.Scanner;
/**
* 回溯法
*/
public class Main {
private static int[] arr = new int[13];
private static int[] count;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
count = new int[9];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
++count[arr[i]-1];
}
int winCount = 0;
// 选择1到9中的一个作为第14张牌,然后判断是否胡牌
for (int i = 1 ; i <= 9; i++) {
if(count[i-1]<4){
++count[i-1];
if(win()){
++winCount;
System.out.print(i);
System.out.print(" ");
}
--count[i-1];
}
}
if(winCount==0){
System.out.println(0);
}
}
public static boolean win(){
// 从1到9 中选择一个作为雀头, 然后判断剩余的牌是否构成4对
for (int i = 1; i <= 9; i++) {
if(count[i-1]<2){
continue;
}
count[i-1]-=2;
if(hasTriples(4)){
count[i-1]+=2;
return true;
}
count[i-1]+=2;
}
return false;
}
public static boolean hasTriples(int n){
if(n==0){
return true;
}
// 1到9,每一张牌尝试三张或顺子
for (int i = 1; i <= 9; i++) {
if(count[i-1]>=3){
count[i-1]-=3;
boolean subHashTriples = hasTriples(n-1);
count[i-1]+=3;
if(subHashTriples){
return true;
}
}
if(i<=7 && count[i-1]>0 && count[i] > 0 && count[i+1]>0){
--count[i-1];
--count[i];
--count[i+1];
boolean subHasTriples = hasTriples(n-1);
++count[i-1];
++count[i];
++count[i+1];
if(subHasTriples){
return true;
}
}
}
return false;
}
}
总结
这题用的是回溯法,看了答案之后也不算太难,刚做时主要不知道如何判断是否有4个顺子或刻子。