#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 记忆化搜索的更多相关文章

  1. uva 707(记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21261 思路:此题需要记忆化搜索,dp[x][y][t]表示当前状 ...

  2. UVa 10118 记忆化搜索 Free Candies

    假设在当前状态我们第i堆糖果分别取了cnt[i]个,那么篮子里以及口袋里糖果的个数都是可以确定下来的. 所以就可以使用记忆化搜索. #include <cstdio> #include & ...

  3. Substring Uva 11468_记忆化搜索 + AC自动机

    Code: #include<cstdio> #include<cstring> #include<queue> using namespace std; cons ...

  4. UVA 10400 Game Show Math (dfs + 记忆化搜索)

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

  5. uva 10581 - Partitioning for fun and profit(记忆化搜索+数论)

    题目链接:uva 10581 - Partitioning for fun and profit 题目大意:给定m,n,k,将m分解成n份,然后依照每份的个数排定字典序,而且划分时要求ai−1≤ai, ...

  6. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  7. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  8. UVa 10285 Longest Run on a Snowboard - 记忆化搜索

    记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...

  9. 状压DP+记忆化搜索 UVA 1252 Twenty Questions

    题目传送门 /* 题意:给出一系列的01字符串,问最少要问几个问题(列)能把它们区分出来 状态DP+记忆化搜索:dp[s1][s2]表示问题集合为s1.答案对错集合为s2时,还要问几次才能区分出来 若 ...

随机推荐

  1. 【转】发布iOS应用程序到苹果APP STORE完整流程

    原文: http://www.cnblogs.com/JuneWang/p/3850859.html 可以为每个app上传5张截图,虽然至少需要上传一张,可能很少有人会只上传一张图片.另外,你还需要分 ...

  2. 菜鸟日记之 java中的集合框架

    java中的集合框架图 如图所示:java中的集合分为两种Collection和Map两种接口 可分为Collection是单列集合和Map的双列集合 Collection单列集合:继承了Iterat ...

  3. 三角网格(Triangle Mesh)的理解

    最简单的情形,多边形网格不过是一个多边形列表:三角网格就是全部由三角形组成的多边形网格.多边形和三角网格在图形学和建模中广泛使用,用来模拟复杂物体的表面,如建筑.车辆.人体,当然还有茶壶等.图14.1 ...

  4. jQuery慢慢啃之工具(十)

    1.jQuery.support//一组用于展示不同浏览器各自特性和bug的属性集合 2.jQuery.browser//浏览器内核标识.依据 navigator.userAgent 判断. 可用值: ...

  5. 从网页psd到html的开发

    从网上下载了一张psd的网页设计稿,初学html+css,所以记录一下我的学习过程.原图是这个样子:             原图                                   ...

  6. js验证手机号码 ,昵称,密码

    手机号 /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/ 传真~  /^(\d{3,4}-)?\d{7,8}$/ 邮箱 ^[a-z0-9]+([._\\ ...

  7. C对字符串的部分操作

    字符串分割(C++)   经常碰到字符串分割的问题,这里总结下,也方便我以后使用. 一.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char ...

  8. Android 网络通信 HTTP

    摘要 1. Http GET 方法访问网站 2. Http POST访问网站 3. HttpClient进行Get方式通信 4. HttpClient进行Post方式通信 -------------- ...

  9. 在IT学习中的“认识论”

    先有“感性认识”,再有“理性认识”.这句经典认识论,在IT领域应用广泛. 一个项目,首先是分析需求,也就是功能层面,这相当于先有“感性认识”:然后才是具体的技术实现,也就是“理性认识”. IT学习分析 ...

  10. ASP.NET MVC轻教程 Step By Step 6——改进表单

    上一节我们使用原始的HTML表单来完成留言功能,但是ASP.NET MVC提供了丰富的HTML辅助方法来帮助我们构建更简洁优雅的表单. Step 1. 修改Form标签 首先,我们可以使用Html.B ...