UVA 10400 Game Show Math (dfs + 记忆化搜索)
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 + 记忆化搜索)的更多相关文章
- dfs+记忆化搜索,求任意两点之间的最长路径
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- 不要62 hdu 2089 dfs记忆化搜索
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)
pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...
- hdu 1078(dfs记忆化搜索)
题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...
- 8636 跳格子(dfs+记忆化搜索)
8636 跳格子 该题有题解 时间限制:2457MS 内存限制:1000K提交次数:139 通过次数:46 题型: 编程题 语言: G++;GCC Description 地上有一个n*m 的数 ...
- poj1088-滑雪 【dfs 记忆化搜索】
http://poj.org/problem?id=1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 79806 ...
- DFS——>记忆化搜索——>动态规划
以洛谷P1802 5倍经验日 为例 https://www.luogu.org/problem/show?pid=1802 题目背景 现在乐斗有活动了!每打一个人可以获得5倍经验!absi2011却 ...
- POJ 1191 棋盘分割 【DFS记忆化搜索经典】
题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
随机推荐
- [ES6] Class Inherit
In constructor, you can call parent's constuctor() method by supert(); class ShoppingCart { construc ...
- Java程序性能分析工具Java VisualVM(Visual GC)—程序员必备利器
VisualVM 是一款免费的\集成了多个JDK 命令行工具的可视化工具,它能为您提供强大的分析能力,对 Java 应用程序做性能分析和调优.这些功能包括生成和分析海量数据.跟踪内存泄漏.监控垃圾回收 ...
- 深入理解PreparedStatement和Statement
执行SQL语句时,就执行一次使用Statement对象,当一句SQL语句要执行多次,这时使用PrepareStatement.虽然使用PrepareStatement执行一次时非内存,但是,在后来的执 ...
- WPF DataGrid 之数据绑定
1. Auto generation of columns 最简单的方法莫过于让DataGrid根据数据源中的字段自动生成列了: 根据实体类的公共属性, 能够自动生成四种类型的数据列,对应关系如下: ...
- 导出Exexcl类
前台: <%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation=" ...
- Dom4j 添加 / 更新 / 删除 XML
1.获得文档 /** *1.获得解析流 *2.解析XML */ 2.添加 /** *1.获取父元素 *2.创建元素 *3.创建属性并添加到元素中 *4.元素添加到根节点 */ 3.更新 /** *1. ...
- hdu5392 Infoplane in Tina Town(LCM)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Infoplane in Tina Town Time Limit: 14000/ ...
- mysql 5.6 参数详解
系统变量提供的是各种与服务器配置和功能有关的信息.大部分的系统变量都可以在服务器启动时进行设置.在运行时,每一个系统变量都拥有一个全局值或会话值,或者同时拥有这两个值.许多系统变量都是动态的,也就是说 ...
- Mysql中natural join和inner join的区别
假设有如下两个表TableA,TableB TableA TableB Column1 Column2 Column1 Column3 1 2 1 3 TableA的Column1列名和TableB的 ...
- CSS太阳月亮地球三角恋旋转效果
纯粹玩一下,好像没有什么实际的卵用,but,纯玩买不了上当,纯玩买不了受骗........ 地月旋转的一个css效果,无聊玩玩,可以复制到记事本试试 <!DOCTYPE html>< ...