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时,还要问几次才能区分出来 若 ...
随机推荐
- tomcat环境变量配置
先下载tomcat的压缩包(绿色版),解压到C盘目录下即可. 在系统坏境中配置(高级系统设置>环境变量>系统变量) 新建变量名:CATALINA_HOME,变量值:C:\apache-to ...
- 5 DML语言
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- Laravel_1 安装
1>http://www.golaravel.com/post/install-and-run-laravel-5-x-on-windows/ 2>http://www.golaravel ...
- C++的函数重载 转
——每个现象后面都隐藏一个本质,关键在于我们是否去挖掘 写在前面: 函数重载的重要性不言而明,但是你知道C++中函数重载是如何实现的呢(虽然本文谈的是C++中函数重载的实现,但我想其它语言也是类似的) ...
- 交叉编译tslib1.4
cross-compiler: arm-linux-gcc V4.2.1 source code: tslib-1.4.tar.gz #tar zxvf tslib-1.4.tar.gz #./aut ...
- 使用PHP脚本来写Daemon程序
什么是Daemon进程 这又是一个有趣的概念,daemon在英语中是"精灵"的意思,就像我们经常在迪斯尼动画里见到的那些,有些会飞,有些不会,经常围着动画片的主人公转来转去,啰 ...
- 编程框架—Autofac
Autofac是一款轻量级的IOC框架,性能高. Autofac基本使用步骤: 1.创建容器建造者(Builder): 2.对Builder注册类型. 3.Buildder创建容器(Container ...
- 下面css hack的写法分别用于哪些浏览器
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- xfire实现webservice客户端之测试关注点
日前的工作接触到很多系统间的Webservice调用,这里想谈谈基于spring+xfire实现的webservice的客户端踩过的一些坑,需要测试关注的点. xFire的配置项 在spring中实现 ...
- iOS:使用导航栏
要求使用ARC // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. ...