雀魂启动!回溯法-Java

import java.util.ArrayList;
import java.util.Scanner;

/**
 * className:test3
 * Package:PACKAGE_NAME
 *
 * @Author:swx
 * @Create2024/1/1113:01
 * @version:1.0
 */
public class test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] state = new int[9], temp = new int[9];
        ArrayList<Integer> res = new ArrayList<>();
        //获取13个数字,统计每个数字出现的次数
        for (int i = 0; i < 13; i++) {
            int num = scanner.nextInt();
            state[num - 1]++;
        }
        // 依次加入新的数字看是否符合要求
        for (int i = 0; i < 9; i++) {
            if (state[i]<4) { // 牌有剩余
                int num = i + 1;
                System.arraycopy(state, 0, temp, 0 ,9);
                temp[i]++;
                if (run(temp, 14, false)) {
                    res.add(num);
                }
            }
        }
        if (res.isEmpty()) {
            System.out.println(0);
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(res.get(0));
            for (int i=1; i<res.size(); i++) {
                stringBuilder.append(" ");
                stringBuilder.append(res.get(i));
            }
            System.out.println(stringBuilder.toString());
        }
    }
    private static boolean run(int[] temp, int count, boolean hasRun) {
        if (count == 0) {
            return true;
        }
        // 判断雀头
        // 遍历个数大于2的数字,让它为雀头
        if (!hasRun) {
            for (int i=0; i<9; i++) {
                if (temp[i]>2) {
                    temp[i] -= 2;
                    // 递归
                    if (run(temp, count-2, true)) {
                        return true;
                    }
                    temp[i] += 2;
                }
            }
            return false;
        } else {
            for (int i=0; i<9; i++) {
                if (temp[i]>0) {
                    // 判断刻子
                    if (temp[i]>=3) {
                        temp[i] -= 3;
                        if (run(temp, count-3, true)) {
                            return true;
                        }
                        temp[i] += 3;
                    }
                    // 判断顺子
                    if (i+2<9 && temp[i+1]>0 &&temp[i+2]>0) {
                        temp[i] -= 1;
                        temp[i+1] -= 1;
                        temp[i+2] -= 1;
                        if (run(temp, count-3, true)) {
                            return true;
                        }
                        temp[i] += 1;
                        temp[i+1] += 1;
                        temp[i+2] += 1;
                    }
                }
            }
        }
        return false;
    }
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值