#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. CI 笔记 easyui 结合后,左侧导航跳转问题

    1. 在进行时,还没有做完整个项目时,可能不是最终稿 2. 从数据库中nav表中,读出url地址,然后,从admin中,重写这些url跳转 3. 在admin的控制器中,跳转写的并不完美, publi ...

  2. 读书笔记之深入分析Java Web技术内幕

    章节: 1 B/SB/S的优点: 客户端使用统一的浏览器(Browser) ,浏览器的统一带来了操作的统一,无论使用什么服务,因为浏览器是相同的,所以操作类似.客户使用简单了.服务端开发简化; 使用统 ...

  3. java设计模式和设计原则

    一.创建型模式 1.抽象工厂模式(Abstract factory pattern): 提供一个接口, 用于创建相关或依赖对象的家族, 而不需要指定具体类.2.生成器模式(Builder patter ...

  4. >炫酷的计时器效果Canvas绘图与动画<

    >炫丽的计时器效果Canvas绘图与动画< 虽然我是学习java的,但是因为最近使用html5的关系,多学习了一下前端知识. 现在,我要介绍的计时器是十分炫酷的,使用画布完成. 喜欢htm ...

  5. strtotime 的几点不同

    在php里面,strtotime()有点比较特殊,date函数也有点问题 if ( date(1,time()) == "Monday")    //似乎 date(1,time( ...

  6. Day20 Django之Model多对多、中间件、缓存、信号和分页

    一.Form补充 class IndexForm(forms.Form): # c = [ # (1, 'CEO'), # (2, 'CTO') # ] # 静态字段,属于IndexForm类,即使数 ...

  7. sql server 查询表某个字段不重复数据

    SELECT TOP (500) ID, Personname, Personcode, Telphone, Fraction into temp3 FROM Records AS a WHERE ( ...

  8. java项目创建和部署

    http://www.cnblogs.com/nexiyi/archive/2012/12/28/2837560.html http://dead-knight.iteye.com/blog/1841 ...

  9. 写个自动下载安装Ant的shell脚本【一】

    #!/bin/bash ###################################################### # file name: install_ant.sh # # f ...

  10. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3119  Solved: 1399[Submit] ...