首页 > 代码库 > nyoj-43 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 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

思路:

       搜索题,比较难想;

       这道题比较容易想复杂超时,个人也是听学长讲过后才明白,思路如下:

       首先将初始值放到数组中,vis数组初始化为0; 每次从未计算的数字中拿出两个来进行六种计算(分别为 a + b, a - b, b - a, a * b, a / b,  b / a,在这儿之所以进行6种运算是为了去除全排列的过程),然后将结果放到数组的的最后一位,标记前边用于计算个两位(标记代表这个数字已经用过了,以后就不能在用了),然后再从后面三个选两个进行6种运算,结果放到最后,以此类推,直到还剩一个有效数字的时候,与目标结果比较,如果相等,搜索结束,否则回溯

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define N 100

bool vis[N];						// 标记 i 位是否有效
double num[N], end;					// num 数组存储数据,end 为最终点数
const double jx = 0.0000001;		// 浮点数判0界限

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

	return -1;
}

bool Jisuan(int ct, int pos)			// 计算函数,ct为数组中剩余有效数字个数,pos为数组终点位置
{
	if(ct == 1){						// 数组中剩余有效数字只有一个时,便与目标比对
		if(fabs(num[pos - 1] - end) < jx)
			return 1;
		else
			return 0;
	}

	for(int i = 0; i < pos; i ++){
		if(vis[i])
			continue;

		vis[i] = 1;
		for(int j = i + 1; j < pos; j ++){
			if(vis[j])
				continue;

			vis[j] = 1;
			for(int k = 0; k < 6; k ++){
				if(fabs(num[i]) < jx && k == 5 || fabs(num[j]) < jx && k == 4)		// 除数不能为0
					continue;

				num[pos] = QiuZhi(num[i], num[j], k);
				if(Jisuan(ct - 1, pos + 1))
					return 1;
				num[pos] = 0;
			}
			vis[j] = 0;
		}
		vis[i] = 0;
	}

	return 0;
}

int main()
{
	int loop, ct, i;
	scanf("%d", &loop);
	while(loop --){
		memset(vis, 0, sizeof(vis));
		scanf("%d%lf", &ct, &end);
		for(i = 0; i < ct; i ++)
			scanf("%lf", &num[i]);

		if(Jisuan(ct, ct))
			printf("Yes\n");
		else
			printf("No\n");
	}

	return 0;
}


nyoj-43 24 Point game (搜索)