UVa 10400 记忆化搜索
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int INF = ;
const int maxn = ; int a[maxn];
char ans[maxn]; //a[i] = 1代表+,2代表-,3代表*,4代表/ ;
int len,goal;
bool vis[maxn][INF+][]; // vis[i][j][1]=1 表示计算完前i个数字的某种组合可以得到正j,dp[i][j][0]=1则为负j bool dfs(int u,int sum){
if(u == ){
if(a[] == sum ) return true;
else return false;
} if(sum >= && vis[u][sum][])
return false; else if(sum< && vis[u][-sum][])
return false; int temp = sum - a[u];
if(temp > -INF && temp < INF){
ans[u] = '+';
if(dfs(u-,temp)) return true;
if(temp >= ){
vis[u-][temp][] = true;
}
else{
vis[u-][-temp][] = true;
}
}
temp = sum + a[u];
if(temp > -INF && temp < INF){
ans[u] = '-';
if(dfs(u-,temp)) return true;
if(temp >= ){
vis[u-][temp][] = true;
}
else{
vis[u-][-temp][] = true;
}
}
temp = sum * a[u];
if(temp > -INF && temp < INF){
ans[u] = '/';
if(dfs(u-,temp)) return true;
if(temp >= ){
vis[u-][temp][] = true;
}
else{
vis[u-][-temp][] = true;
}
}
if(sum % a[u]) return false;
temp = sum / a[u];
if(temp > -INF && temp < INF){
ans[u] = '*';
if(dfs(u-,temp)) return true;
if(temp >= ){
vis[u-][temp][] = true;
}
else{
vis[u-][-temp][] = true;
}
}
return false;
}
void print(){
ans[len+] = '=';
for(int i=;i<=len;i++){
printf("%d%c",a[i],ans[i+]);
}
printf("%d\n",goal);
}
int main()
{
//freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
while(T--){
scanf("%d",&len);
for(int i=;i<=len;i++)
scanf("%d",&a[i]);
scanf("%d",&goal); memset(vis,,sizeof(vis)); if(dfs(len,goal)){
print();
}
else
printf("NO EXPRESSION\n");
}
}
UVa 10400 记忆化搜索的更多相关文章
- uva 707(记忆化搜索)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21261 思路:此题需要记忆化搜索,dp[x][y][t]表示当前状 ...
- UVa 10118 记忆化搜索 Free Candies
假设在当前状态我们第i堆糖果分别取了cnt[i]个,那么篮子里以及口袋里糖果的个数都是可以确定下来的. 所以就可以使用记忆化搜索. #include <cstdio> #include & ...
- Substring Uva 11468_记忆化搜索 + AC自动机
Code: #include<cstdio> #include<cstring> #include<queue> using namespace std; cons ...
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
Problem H Game Show Math Input: standard input Output: standard output Time Limit: 15 seconds A game ...
- uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)
题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...
- UVA - 10118Free Candies(记忆化搜索)
题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
- UVa 10285 Longest Run on a Snowboard - 记忆化搜索
记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...
- 状压DP+记忆化搜索 UVA 1252 Twenty Questions
题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...
随机推荐
- 额定能量不得超过160Wh, 等同是多少mAh电池容量?
额定能量不得超过160Wh, 等同是多少mAh电池容量?行动电源容量标示, 正确应该是用Whr(Wh)瓦特小时来标示, 不过坊间标榜行动电源的容量通常是用xx000mAhWHr瓦特小时, 即是行动电源 ...
- FTPClient文件下载
一.FTPClient下载文件所需要的jar包: org.apache.commons.net [commons-net-3.4.jar] 二.FTPClient的连接和关闭 //FTPClient ...
- SQL反模式部分内容笔记
规范化: 1, 以一种我们能够理解的方式表达这个世界中的事物; 2, 减少数据冗余存储, 防止异常或者不一致的数据; 3, 支持完整性约束. Tips: 提高数据的性能不在此列表中. 意义: 规范化 ...
- ZOJ 1057 Undercut(简单模拟)
Undercut 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=57 题目大意:a card game,two pla ...
- SGU 269. Rooks(DP)
题意: 给n(<=250)条水平网格,然后在上面放k棋子,每行每列都只能放一个.求方法总数. Solution: 简单的DP, 只要对给出的水平长度排个序就很容易处理了. 需要用到高精度. 偷懒 ...
- Excel里面将头尾第一个字母保留,去除中间的用*号代替,主要是REPT函数的应用,一开始我还以为要自己写个自定义函数
Excel里面将头尾第一个字母保留,去除中间的用*号代替,主要是REPT函数的应用,一开始我还以为要自己写个自定义函数 =LEFT(A1,1)&REPT("*",(LEN( ...
- [转] 小tips: 使用 等空格实现最小成本中文对齐 ---张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4562 一.重见天日第 ...
- Ubuntu14.04不支持U盘exfat格式该如何解决
转: http://www.jb51.net/os/Ubuntu/275158.html exfat是U盘的文件系统,很多系统都支持exfat格式的使用,但Ubuntu系统并不支持exfat格式,要如 ...
- WPF学习之路初识
WPF学习之路初识 WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...
- C# Json反序列化处理
最近换工作了 从客户端转到Web端 第一个任务就是去别人的页面上抓取数据 用到的是JSON 因为他们网站json的格式有点怪 所以 就在JSON反序列化上面 花了一点时间 首先用到的工具是http:/ ...