nyoj-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 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

题意:

有一个游戏叫做24分游戏。
在这个游戏中,你会得到一些数字。你的任务是找到一个包含所有给定数字的表达式,这个表达式的值应该是24。这个表达式除了加、减、乘、除和括号外,不能有其他运算符。
如果你所给的数字是“3 3 8 8”,你可以用“8/(3-8/3)”来回答。应该使用所有的数字,并且可以用括号
在这个问题中,您的任务仅仅是判断给定的数字是否可以用于查找其值为给定数字的表达式。
输入
输入有多个实例,每个实例包含一行
输入的第一行是一个非负整数C(C<=100),它表示用例的数量。
每一行都有一些整数,第一个整数M(0<=M<=5)是给定数字组成表达式的总数,第二个整数N(0<=N<=100)是表达式的值应该是的数字。
然后,接下来的M整数是给定的数字。所有给定的数字都是非负且小于100的
输出
对于每个测试用例,如果有一个符合所有需求的表达式,则输出“Yes”,否则输出“No”。

简要意思:给你几个数,必须全部用上,只能用加减乘除,判读能不能得到给出的数字m,

**思路:**典型的深度优先搜索,用数组num[] 保存n个数,以及每次搜索运算产生的结果,

#include "iostream"
#include "cstring"
#include "cmath"
#include "algorithm"
using namespace std;
double num[20];//保留 n个数 以及他们预算产生的结果 
bool vis[20];//记录num数组对应的数是否使用过 
int n;
double m;
int dfs(int user,int total) //user表示参与计算数的个数,total表示上次计算的最终结果在Num数组的下标 
{
	int i,j;
	if(user==n) //使用了n个数 
	{
		if(fabs(num[total-1]-m)<0.000001) //判断本次深度搜索的结果是否等于m 
			return 1;
		else
			return 0;
	}
	for(i=0;i<total-1;i++) //每个数都尝试深度搜索一遍 
	{
		if(vis[i]) //如果该数已经使用了,尝试下一个数 
			continue;
		vis[i]=true;//先将其标记为使用,下面对num[i]进行搜索 
		for(j=i+1;j<total;j++)
		{
			if(vis[j])
				continue;
			vis[j]=true;
			num[total]=num[i]+num[j];if(dfs(user+1,total+1)) return 1;
			num[total]=num[i]-num[j];if(dfs(user+1,total+1)) return 1;
			num[total]=num[j]-num[i];if(dfs(user+1,total+1)) return 1;//减法两种情况 
			num[total]=num[i]*num[j];if(dfs(user+1,total+1)) return 1;
			//除法也有两种情况 
			if(num[j]) 
			{
				num[total]=num[i]/num[j];if(dfs(user+1,total+1)) return 1;
			}
			if(num[i]) 
			{
				num[total]=num[j]/num[i];if(dfs(user+1,total+1)) return 1;
			}
			vis[j]=false;//搜索失败,清除标记,方便下次搜索使用 
		}
		vis[i]=false;//搜索失败,清除标记,方便下次搜索使用 
	}
	return 0;
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i=0;i<n;i++)
			cin>>num[i];
		memset(vis,false,sizeof(vis)); //重置vis数组 
		if(dfs(1,n))
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值