深搜 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 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
来源
经典改编
上传者

张云聪

题目不难,只有四种可能,逐个遍历搜索就行!

代码如下:

//考查知识点:深搜
//因为全角,半角的输入问题,看了一个小时。。。
#include<stdio.h>
#include<math.h>
#include<string.h>
#define eps 10E-6
int n;
double sum;
double a[110];
int dfs(int num)
{
	if(num==n)
	{
		if(fabs(a[n]-sum)<=eps)//跳出条件
		return 1;
		return 0;
	}
	for(int i=num;i<n;++i)//从num开始保证了深度遍历的时候,数字逐步后移
	{
		for(int j=i+1;j<=n;++j)
		{
			double right,left;
			left=a[i];
			right=a[j];
			a[i]=a[num];//将值赋值给数组 

			a[j]=left+right;//遍历每一种可能的操作
			if(dfs(num+1))
			return 1;

			a[j]=left-right;//大小不确定
			if(dfs(num+1))
			return 1;

			a[j]=-left+right;
			if(dfs(num+1))
			return 1;

			a[j]=left*right;
			if(dfs(num+1))
			return 1;

			if(right)
			a[j]=left/right;//除数不为零
			if(dfs(num+1))
			return 1;

			if(left)
			a[j]=right/left;
			if(dfs(num+1))
			return 1;

			a[i]=left;//回溯
			a[j]=right;
		}
	}
	return 0;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%lf",&n,&sum);
		for(int i=1;i<=n;++i)
		{
			scanf("%lf",&a[i]);
		}
		if(dfs(1))
		puts("Yes");
		else
		puts("No");
	}
	return 0;
} 
时间: 2024-10-28 19:08:46

深搜 nyoj 43 24 Point game的相关文章

nyoj 43 24 Point game(dfs暴力)

描述 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 o

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

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 expr

DFS [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

NYoj 素数环(深搜入门)

题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=488 深搜模板: 1 void dfs(int 当前状态) 2 { 3 if(当前状态为边界状态) 4 { 5 记录或输出 6 return; 7 } 8 for(i=0;i<n;i++) //横向遍历解答树所有子节点 9 { 10 //扩展出一个子状态. 11 修改了全局变量 12 if(子状态满足约束条件) 13 { 14 dfs(子状态) 15 } 16 恢复全局变量//回

NYOJ 迷宫寻宝(一)深搜

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=82 做这道题的时候迷迷糊糊的,,果然比较难..最后也是没有做出来..请教了一下学长,学长说我基础还不好..基础果然重要,这道题是一道搜索题,我没有考虑钥匙在门后面的情况,比如aBbSAG 多亏学长指教,通过这道题对深搜的理解又加深了一步~ #include <iostream> #include <cstdio> #include <cstring> using

NYOJ 293 Sticks 【深搜】+【剪枝】

这是一道让人泪奔的题,它深刻的说明了什么是剪枝,哪怕是再小的一个细节,一旦递归规模增大都会引发巨大的时间消耗,真是神题~ Sticks 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the

NYOJ 10 skiing (深搜和动归)

skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每一个数字代表点的高度.以下是一个样例 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9

nyoj 488 素数环(深搜)

素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输入 有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束. 输出 每组第一行输出对应的Case序号,从1开始. 如果存在满足题意叙述的素数环,从小到大输出. 否则输出No Answer. 样例输入 6 8 3 0 样