NYOJ43 24 Point game 【回溯】

24 Point game

时间限制:3000 ms  |  内存限制:65535 KB

难度:5

描述

There is a game which is called 24 Point game.

In this game , you will be given some numbers. Your task is to find an expression which have all the given numbers and the value of the expression should be 24 .The expression mustn‘t have any other operator except plus,minus,multiply,divide and the brackets.

e.g. If the numbers you are given is "3 3 8 8", you can give "8/(3-8/3)" as an answer. All the numbers should be used and the bracktes can be nested.

Your task in this problem is only to judge whether the given numbers can be used to find a expression whose value is the given number。

输入
The input has multicases and each case contains one line

The first line of the input is an non-negative integer C(C<=100),which indicates the number of the cases.

Each line has some integers,the first integer M(0<=M<=5) is the total number of the given numbers to consist the expression,the second integers N(0<=N<=100) is the number which the value of the expression should be.

Then,the followed M integer is the given numbers. All the given numbers is non-negative and less than 100

输出
For each test-cases,output "Yes" if there is an expression which fit all the demands,otherwise output "No" instead.
样例输入
2
4 24 3 3 8 8
3 24 8 3 3
样例输出
Yes
No

这题的思路是把n个数字任意两两按照+-*/组合,直到只剩一个数时与目标数比较是否相等即可,开始因为cal函数的返回值写成了int而WA了一次,很亏,以后要注意。

#include <stdio.h>
#include <math.h>
int t, n;
double targ, arr[10];
bool vis[10];

double cal(double a, double b, int k){
	switch(k){
		case 0: return a + b;
		case 1: return a - b;
		case 2: return a * b;
		case 3: return a / b;
		case 4: return b - a;
		case 5: return b / a;
	}
}

bool backTrack(int num, int pos){
	if(num == 1 && fabs(arr[pos] - targ) < 1e-5) return true;
	double tmp1, tmp2;

	for(int i = 0; i < n; ++i){
		if(!vis[i]) for(int j = i + 1; j < n; ++j){
			if(!vis[j]){
				tmp1 = arr[i]; tmp2 = arr[j];
				for(int k = 0; k < 6; ++k){
					if(k == 3 && arr[j] == 0 || k == 5 && arr[i] == 0) continue;
					arr[j] = cal(arr[i], arr[j], k);
					vis[i] = true;
					if(backTrack(num - 1, j)) return true;
					vis[i] = false;
					arr[i] = tmp1; arr[j] = tmp2;
				}
			}
		}
	}
	return false;
}

int main(){
	scanf("%d", &t);
	while(t--){
		scanf("%d%lf", &n, &targ);
		for(int i = 0; i < n; ++i)
			scanf("%lf", &arr[i]);
		for(int i = 0; i < 10; ++i) vis[i] = false;
		printf(backTrack(n, n - 1) ? "Yes\n" : "No\n"); //n个元素,结束位置是n-1
	}
	return 0;
}

NYOJ43 24 Point game 【回溯】

时间: 2024-10-05 14:48:52

NYOJ43 24 Point game 【回溯】的相关文章

nyoj-43 24 Point game (搜索)

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is called 24 Point game. In this game , you will be given some numbers. Your task is to find an expression which have all the given numbers and the value of the expression sho

[LeetCode] 679. 24 Game(回溯法)

传送门 Description You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, )to get the value of 24. Example 1: Input: [4, 1, 8, 7] Output: True Explanation: (8-4) * (7-1) = 24 Example 2

NYOJ-43 24 Point game-DFS

24 Point game 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 There is a game which is called 24 Point game. In this game , you will be given some numbers. Your task is to find an expression which have all the given numbers and the value of the expression sho

1.7 试探法(回溯法)

生成彩票数字问题 1 #include <stdio.h> 2 #define MAXN 7 //设置每一注彩票的位数 3 #define NUM 29 //设置组成彩票的数字 4 int num[NUM]; 5 int lottery[MAXN]; //每一注彩票的每一位数 6 void combine(int n, int m) 7 { 8 int i,j; 9 for(i=n;i>=m;i--) 10 { 11 lottery[m-1]=num[i-1]; //保存一位数字 12

LeetCode51 N皇后——经典dfs+回溯(三段式解法)

代码如下: 1 class Solution { 2 public: 3 // record[row] 该行对应的列 4 vector<vector<string> > ans; // 结果集 5 vector<vector<string>> solveNQueens(int n) { 6 string s = ""; 7 for(int i=0; i<n; i++){ 8 s += '.'; 9 } 10 vector<in

Leetcode之深度优先搜索&amp;回溯专题-679. 24 点游戏(24 Game)

深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24. 示例 1: 输入: [4, 1, 8, 7] 输出: True 解释: (8-4) * (7-1) = 24 示例 2: 输入: [1, 2, 1, 2] 输出: False 注意: 除法运算符 / 表示实数除法,而不是整数除法.例如 4 / (1 - 2/3) = 12 . 每个运算符对两个数进行运算.特别是我们不能用 - 作为一元运算符.例如,[1, 1

五大常用算法之四:回溯法

(转自:http://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741376.html) 1.概念 回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回,尝试别的路径. 回溯法是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择,这种走不通就退回再走的技术为回溯法,而满足回溯条件的某个状态的点称为“回溯点”. 许

回溯0--递归回溯算法框架

递归回溯算法框架 一.心得 3 都是在for下的if下的 4 保存结果,找下一步,回溯,这三个是一起的 5 还有一个到达目的地输出解放在外面就好 search后面的k是轮数  三个数组:原数据数组标记数组储存结果数组 框架二 到目的地的情况要多加1,因为满足了的下一轮就好判断 二.代码 1 /* 2 递归回溯算法框架: 3 都是在for下的if下的 4 保存结果,找下一步,回溯,这三个是一起的 5 还有一个到达目的地输出解放在外面就好 6 */ 7 /* 8 框架一 9 */ 10 int se

栈回溯技术

转载于:http://blog.csdn.net/yangzhiloveyou/article/details/9042137 1.    前言 段错误.非法地址访问等问题导致程序崩溃的现象屡屡发生,如果能找到发生错误的函数,往往一眼就能看出BUG所在--对于这类比较简单的问题,比如使用空指针进行读写等,利用栈回溯技术可以很快定位.但是对于数组溢出.内存泄漏等问题导致的程序错误,往往隐藏很深,它们并不当场发作,即使我们一步一步跟踪到发生错误的语句时,也经常会让人觉得"这个地方根本不可能出错啊&q