Problem H
Game Show Math
Input:
 standard input
Output: standard output
Time Limit: 15 seconds

A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target number. The contestant must make a mathematical expression using all of the numbers in the sequence and only the operators: +-*, and, /. Each number in the sequence must be used exactly once, but each operator may be used zero to many times. The expression should be read from left to right, without regard for order of operations, to calculate the target number. It is possible that no expression can generate the target number. It is possible that many expressions can generate the target number.

There are three restrictions on the composition of the mathematical expression:

o  the numbers in the expression must appear in the same order as they appear in the input file

o  since the target will always be an integer value (a positive number), you are only allowed to use / in the expression when the result will give a remainder of zero.

o  you are only allowed to use an operator in the expression, if its result after applying that operator is an integer from (-32000 ..+32000).

Input

The input file describes multiple test cases. The first line contains the number of test cases n.

Each subsequent line contains the number of positive numbers in the sequence p, followed by p positive numbers, followed by the target number. Note that 0 < p £ 100. There may be duplicate numbers in the sequence. But all the numbers are less than 32000.

Output

The output file should contain an expression, including all k numbers and (k-1) operators plus the equals sign and the target. Do not include spaces in your expression. Remember that order of operations does not apply here. If there is no expression possible output "NO EXPRESSION" (without the quotes). If more than one expression is possible, any one of them will do.

 

Sample Input

3
3 5 7 4 3
2 1 1 2000
5 12 2 5 1 2 4

Sample Output
5+7/4=3 
NO EXPRESSION
12-2/5*1*2=4

题意:给定n个数字。和一个answer。要求在n个数字中插入“+-*/“ 使得式子成立。。

题目给了几个限定条件:1、数字位置不能变,且运算符优先级与先后顺序有关(和+-*/无关)。。

2、运算过程结果必须一直保持在[-32000,32000]之间。

3、‘/’号只能在整除的情况下才能使用。。

思路:直接暴力时间复杂度O(4 ^ n)。这题最多100。。果断超时的节奏。。于是乎用记忆化搜索。

开一个二维数组vis[i][j].i表示加到第几个,j表示当前结果。。如果重复状态直接return。

这里有一个要注意的地方。就是j表示的当前结果可能为负数。但是负数最多为-32000。。所以我们可以把结果都加上32000.这样就没有问题了。

代码:

#include <stdio.h>
#include <string.h> int vis[105][66666];
int t, n, num[105], ans, judge;
char out[105];
void dfs(int start, int sum) {
if (start == n) {
if (sum == ans)
judge = 1;
return;
}
if (sum + num[start] <= 32000 && !vis[start][sum + num[start] + 32000]) {
out[start] = '+';
vis[start][sum + num[start] + 32000] = 1;
dfs(start + 1, sum + num[start]);
if (judge) return;
}
if (sum - num[start] >= -32000 && !vis[start][sum - num[start] + 32000]) {
out[start] = '-';
vis[start][sum - num[start] + 32000] = 1;
dfs(start + 1, sum - num[start]);
if (judge) return;
}
if (sum * num[start] >= -32000 && sum * num[start] <= 32000 && !vis[start][sum * num[start] + 32000]) {
out[start] = '*';
vis[start][sum * num[start] + 32000] = 1;
dfs(start + 1, sum * num[start]);
if (judge) return;
}
if (!(sum % num[start])) {
if (sum / num[start] >= -32000 && sum / num[start] <= 32000 && !vis[start][sum / num[start] + 32000]) {
out[start] = '/';
vis[start][sum / num[start] + 32000] = 1;
dfs(start + 1, sum / num[start]);
if (judge) return;
}
}
}
int main() {
scanf("%d", &t);
while (t --) {
judge = 0;
memset(vis, 0, sizeof(vis));
scanf("%d", &n);
for (int i = 0; i < n; i ++)
scanf("%d", &num[i]);
scanf("%d", &ans);
vis[0][num[0] + 32000] = 1;
dfs(1, num[0]);
if (judge) {
for (int i = 0; i < n; i ++) {
if (i != 0)
printf("%c%d", out[i], num[i]);
else
printf("%d", num[i]);
}
printf("=%d\n", ans);
}
else printf("NO EXPRESSION\n");
}
return 0;
}

UVA 10400 Game Show Math (dfs + 记忆化搜索)的更多相关文章

  1. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  2. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  3. kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  5. hdu 1078(dfs记忆化搜索)

    题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...

  6. 8636 跳格子(dfs+记忆化搜索)

    8636 跳格子 该题有题解 时间限制:2457MS  内存限制:1000K提交次数:139 通过次数:46 题型: 编程题   语言: G++;GCC Description 地上有一个n*m 的数 ...

  7. poj1088-滑雪 【dfs 记忆化搜索】

    http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 79806 ...

  8. DFS——>记忆化搜索——>动态规划

    以洛谷P1802  5倍经验日 为例 https://www.luogu.org/problem/show?pid=1802 题目背景 现在乐斗有活动了!每打一个人可以获得5倍经验!absi2011却 ...

  9. POJ 1191 棋盘分割 【DFS记忆化搜索经典】

    题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

随机推荐

  1. 大数据笔记09:大数据之Hadoop的HDFS使用

    1. HDFS使用: HDFS内部中提供了Shell接口,所以我们可以以命令行的形式操作HDFS

  2. JavaScripts学习日记——DOM

    DOM Document Object Model 文档对象模型  整合js和html css.控制html文档行为.DOM就是把页面当中所有内容全部封装成对象.HTML文档中万物皆对象.1.对象的分 ...

  3. OD: Big_Endian vs Little_Endian

    经调试,Windows 下为 Little_Endian,OD 中堆栈数据区的 (dword)0xAABB0102,0x02 存储在低地址,0x01 存储在高地址. 内容来自:http://blog. ...

  4. 从html字符串中获取div内容---jquery

    思考的问题: 怎么在一个网页的div中嵌套另外的网页(不使用inclue,iframe和frame,不使用他们的原因,include只能嵌套静态网页,iframe对网络爬虫影响,frame嵌套网页无法 ...

  5. Directory.GetFiles 方法

    Directory.GetFiles 方法 返回指定目录中文件的名称(包括其路径). 命名空间:   System.IO程序集:  mscorlib(mscorlib.dll 中) Enumerate ...

  6. Map Resource Definition 中type为 'ArcGIS Server Local'.

    在使用 MapResourceManager, Map进行 地图显示 时, 在 Map Resource Definition 的 type 为 ArcGIS Server Local时概述. 1,打 ...

  7. cocos2dx SpriteBatchNode 精灵的渲染优化类

    用处是减少对精灵的渲染次数,用法如下 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("person.plist&qu ...

  8. MVC校验特性

    1.前端引入3个脚本       ①jq脚本   ②jQuery.Validate.js  ③jquery.validate.unobtrusive.js(异步验证) 2.后端加特性 在表对应的Mod ...

  9. Tiling_easy version(填2 x N的格子的种类)

    E - Tiling_easy version 题目大意: 有一个大小是 2 x n 的网格,现在需要用2种规格的骨牌铺满,骨牌规格分别是 2 x 1 和 2 x 2,请计算一共有多少种铺设的方法. ...

  10. Go语言之defer

    defer语句被用于预定对一个函数的调用.我们把这类被defer语句调用的函数称为延迟函数.注意,defer语句只能出现在函数或方法的内部. 一条defer语句总是以关键字defer开始.在defer ...