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;
}
时间: 2024-10-10 02:32:50

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

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

hdu 1427 速算24点(next_permutation 搜索)

题意:给出四张扑克牌 问能否算出24 思路:http://blog.csdn.net/xingyeyongheng/article/details/11137631 其实这题只有两种运算顺序 1([email protected])@[email protected] 2 ([email protected])@([email protected]) 所以只需要把数字和运算符全排列遍历一边就可以判断出结果 #include<cstdio> #include<cstring> #in

【算法学习笔记】24.记忆化搜索 解题报告 SJTU_OJ 1002 二哥种花生

在吴学长的代码上做了一点简化修改,本质一样.在外面铺了一圈0,让代码更简单一点,不用考虑边界情况了. 题目: http://acm.sjtu.edu.cn/OnlineJudge/problem/1002 #include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { int L,W; cin>>L>>W; int** su

深度优先搜索(迷宫救人最短路径)

1 import java.util.Scanner; 2 3 public class One { 4 //n,m为迷宫的行列范围,p,q是某人迷路所在地点,min用于记录走到终点最小路径的步数 5 public static int n,m,p,q,min=9999; 6 //数组a是迷宫,1代表有障碍物,数组d用于移动方向(右.下.左.上),数组book用于标记当前位置是否在路径中 7 public static int a[][]=new int[51][51],book[][]=new

百度地图,根据给定坐标,显示、搜索、查询路线

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <style type="text/css"> 6 body,html,#allmap { 7 width: 100%; 8 height: 100%; 9 overfl

使用Lucene.NET实现站内搜索

使用Lucene.NET实现站内搜索 导入Lucene.NET 开发包 Lucene 是apache软件基金会一个开放源代码的全文检索引擎工具包,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎.Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎.Lucene.Net 是 .NET 版的Lucene. 你可以在这里下载到最新的Lucene.NET 创建索引.更新索引.删除索引 搜索