#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. BearSkill实用方法之UITextField限制输入的字符数量

    原文:http://blog.csdn.net/xiongbaoxr/article/details/51525061      

  2. Error Domain=com.google.greenhouse Code=-102

    *** Terminating app due to uncaught exception 'com.google.greenhouse', reason: 'Error Domain=com.goo ...

  3. 打印出不同顺序的字符串&单引号和双引号的差异

    发现一个很好玩的打印顺序 package com.liaojianya.chapter1; /** * This program demonstrates the string. * @author ...

  4. vs2013 使用vs调试器,发现调试器显示的数据错误。查看内存,发现内存是正确的。

    有可能只是调试器的问题,程序可以正常运行的! 网上没找到此种情况解释.感觉有可能是那里堆被破坏了.

  5. Bresenham画直线,任意斜率

    function DrawLineBresenham(x1,y1,x2,y2) %sort by x,sure x1<x2. if x1>x2 tmp=x1; x1=x2; x2=tmp; ...

  6. ACM Sdut 2158 Hello World!(数学题,排序) (山东省ACM第一届省赛C题)

    题目描述 We know thatIvan gives Saya three problems to solve (Problem F), and this is the firstproblem. ...

  7. __init__ __new__区别

    请运行代码: class A: def __init__(self): print "A.__init" def __new__(self): print "A.__ne ...

  8. HTML5 程序设计笔记(二)

    Canvas API 1.HTML5 Canvas 概述 1.1 历史 Canvas的概念最初是由苹果公司提出的,用于在Mac OS X WebKit中创建控制板部件(dashboard widget ...

  9. centos下配置多个tomcat同时运行

    首先安装好jdk,下载好tomcat,我的是apache-tomcat-7.0.50,不用专门配置CATALINA_2_BASE,CATALINA_2_HOME等环境变量. 把tomcat解压到lin ...

  10. Vive开发教程汇总

    最近在整理在HTC Vive平台上开发VR应用程序的教程,现在把结果全部汇总在下面的表格里,希望更多的开发者参与到VR内容的开发之中,真的很好玩.现在主流的开发VR应用的引擎是Unity3D和Unre ...