【算法练习】24点游戏算法


24点游戏算法

【题目描述】
给出4个1-10的数字,通过加减乘除,得到数字为24就算胜利
【输入】
4个1-10的数字。[数字允许重复,但每个数字仅允许使用一次,测试用例保证无异常数字。]
【输出】
true or false
本题含有多组样例输入。
【输入描述】
输入4个int整数
【输出描述】
返回能否得到24点,能输出true,不能输出false

【样例输入】
7 2 1 10
【样例输出】
true


测试网址: https://www.nowcoder.com/practice/fbc417f314f745b1978fc751a54ac8cb?tpId=37&&tqId=21290&rp=1&ru=/ta/huawei&qru=/ta/huawei/question-ranking


个人解法

【解法一】
这种解法的思路是模拟两两组合,例如7 2 1 10,第一次组合有(7和2),(7和1),(7和10),(2和1), (2和10),(1和10),每种组合都有4种情况(加减乘除),当第一次组合为(7和2)中的(7×2)时,那么第二次组合有(14和1),(14和10),(1和10),…
代码:


import java.util.Scanner;

public class _DFS_24点游戏算法 {
	static double[] r;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			r = new double[4];
			vis = new boolean[4];
			for (int i = 0; i < 4; ++i) 
				r[i] = sc.nextInt();
			System.out.println(dfs(4));
		}
	}

	static boolean[] vis;
	public static boolean dfs(int x) {
        double t;
		for (int i = 0; i < 4; ++i) {		//i下标元素用作被组合,即组合后的结果保存在i下标
			if (vis[i]) continue;	//检测该下标的元素是否组合过
			for (int j = i + 1; j < 4; ++j) {		//j下标元素用作组合
				if (vis[j]) continue;	//检测该下标的元素是否组合过
				vis[j] = true;		//记录当前j下标已经组合
				t = r[i];
				//检测两两组合的四种情况能否组合出24
				if(Math.abs(t+r[j] - 24) < 0.00001 || Math.abs(t-r[j] - 24) < 0.00001 || Math.abs(t*r[j] - 24) < 0.00001 || Math.abs(t/r[j] - 24) < 0.00001) 	
					return true;
				r[i] = t+r[j];
				if(dfs(x - 1)) return true;
				r[i] = t-r[j];
				if(dfs(x - 1)) return true;
				r[i] = t*r[j];
				if(dfs(x - 1)) return true;
				r[i] = t/r[j];
				if(dfs(x - 1)) return true;
				r[i] = t;
				vis[j] = false;		//还原j下标的组合情况
			}
		}
		return false;
	}
	
}

【解法二】
这种解法是将输入的4个数字进行全排列(全排列是指所有可能的排列方式),然后从左到右逐个组合,每次组合有4种情况。
代码:


import java.util.Scanner;

public class _DFS_24点游戏算法 {
	static double[] r;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			r = new double[4];
			res = false;
			for(int i=0; i<4; ++i)
				r[i] = sc.nextInt();
			qpl(0);
			System.out.println(res);
		}
	}

	static boolean res;
	public static void qpl(int x) {
		if(x == r.length-1) {
			res = dfs(r[0], 1);
			return ;
		}
		for(int i=x; i<r.length && !res; ++i) {
			swap(x, i);
			qpl(x+1);
			swap(i, x);
		}
	}

	public static boolean dfs(double sum, int x) {
		if(Math.abs(sum-24)<0.00001) return true;
		if(x == 4) return false;
		return dfs(sum+r[x], x+1)||dfs(sum-r[x], x+1)||dfs(sum*r[x], x+1)||dfs(sum/r[x], x+1);
	}

	public static void swap(int a, int b) {
		double t = r[a];
		r[a] = r[b];
		r[b] = t;
	}
	
}

解法对比: 由于数据范围小,所以两种解法所消耗的时间和空间差别不大,但第一种有着一个独有的特点,其能够实现“括号优先计算”的功能,可以想象为每次组合为当前两个元素存在于同一个括号中。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值