UVA 10564 - Paths through the Hourglass (dp)
本文出自 http://blog.csdn.net/shuangde800
f[i][j][k] 代表从(i,j)点往下走到最后一层和为k的方案数
那么,显然可以得到状态转移:
f[i][j][k] = f[i+1][left][k-val] + f[i+1][right][k-val], val=(i,j)格上的数字,left是往坐下走的坐标,right往右下走的坐标
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @author: shuangde
* @blog: blog.csdn.net/shuangde800
* @email: zengshuangde@gmail.com
*===========================================*/ #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std; typedef long long int64;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0); int n, s;
int hourGlass[50][22];
int64 f[50][22][510]; void input(){
for(int i=1; i<=n; ++i)
for(int j=1; j<=n-i+1; ++j)
scanf("%d", &hourGlass[i][j]); for(int i=n+1; i<=2*n-1; ++i)
for(int j=1; j<=i+1-n; ++j)
scanf("%d", &hourGlass[i][j]); } void print_path(int i, int j, int sum){
if(i >= 2*n-1) return;
int val = hourGlass[i][j];
if(i<n){
if(j>1 && f[i+1][j-1][sum-val]){
printf("L");
print_path(i+1, j-1, sum-val);
return ;
}
printf("R");
print_path(i+1, j, sum-val); }else{
if(f[i+1][j][sum-val]){
printf("L");
print_path(i+1, j, sum-val);
return;
}
printf("R");
print_path(i+1, j+1, sum-val);
}
} int main(){ while(~scanf("%d%d", &n, &s) && n+s){ input();
memset(f, 0, sizeof(f)); // 初始化最下面一行
for(int i=1; i<=n; ++i)
f[2*n-1][i][hourGlass[2*n-1][i]] = 1; // 下半部分dp
for(int i=2*n-2; i>=n; --i){
for(int j=1; j<=i+1-n; ++j){
for(int v=hourGlass[i][j]; v<=s; ++v){
int w = hourGlass[i][j];
f[i][j][v] = f[i+1][j][v-w] + f[i+1][j+1][v-w];
}
}
} // 上半部分dp
int64 ans = 0;
for(int i=n-1; i>=1; --i){
for(int j=1; j<=n-i+1; ++j){
for(int v=hourGlass[i][j]; v<=s; ++v){
int w = hourGlass[i][j];
if(j>1) f[i][j][v] += f[i+1][j-1][v-w];
if(j<n-i+1) f[i][j][v] += f[i+1][j][v-w];
}
if(i==1) ans += f[1][j][s];
}
} cout << ans << endl;
for(int i=1; i<=n; ++i){
if(f[1][i][s]){
printf("%d ", i-1);
print_path(1, i, s);
break;
}
}
puts(""); }
return 0;
}
UVA 10564 - Paths through the Hourglass (dp)的更多相关文章
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- 01背包(类) UVA 10564 Paths through the Hourglass
题目传送门 /* 01背包(类):dp[i][j][k] 表示从(i, j)出发的和为k的方案数,那么cnt = sum (dp[1][i][s]) 状态转移方程:dp[i][j][k] = dp[i ...
- UVA 10564 Paths through the Hourglass(背包)
为了方便打印路径,考虑从下往上转移.dp[i][j][S]表示在i行j列总和为S的方案, dp[i][j][S] = dp[i+1][left][S-x]+dp[i+1][right][S-x] 方案 ...
- UVA - 10564 Paths through the Hourglass
传送门:https://vjudge.net/problem/UVA-10564 题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前 ...
- UVA 10564_ Paths through the Hourglass
题意: 由0-9的数字组成一个形如沙漏的图形,要求从第一行开始沿左下或者右下到达最后一行,问有多少种不同的路径,使最后路径上的整数之和为给定的某个数. 分析: 简单计数dp,从最后一行开始,设dp[i ...
- UVA 10564 十 Paths through the Hourglass
Paths through the Hourglass Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- uva 10564
Problem FPaths through the HourglassInput: Standard Input Output: Standard Output Time Limit: 2 Seco ...
- UVA 10163 Storage Keepers(两次DP)
UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...
- uva 11584 Partitioning by Palindromes 线性dp
// uva 11584 Partitioning by Palindromes 线性dp // // 题目意思是将一个字符串划分成尽量少的回文串 // // f[i]表示前i个字符能化成最少的回文串 ...
随机推荐
- c#执行并行任务之Parallel与TaskFactory
任务:几千条(大量)数据往服务器数据库填写.要求单开线程执行,分割成小数据包,多线程运行. 实现方法:Parallel与TaskFactory都可以. 主要代码: Parallel: Barrier ...
- WCF - Versus Web Service
There are some major differences that exist between WCF and a Web service which are listed below. 这里 ...
- PowerStack
int curInc; HashMap<Integer, Integer> incMap; Stack<Integer> stack; public SuperStack() ...
- NOI2014 起床困难综合症
3668: [Noi2014]起床困难综合症 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 225 Solved: 153[Submit][Stat ...
- 对XX证券报关于物联网操作系统的几个问题的答复
XX证券报提问了几个关于物联网和物联网操作系统的问题,个人表达了一些粗陋的观点,在这里发表出来,与行业朋友交流和探讨. 物联网行业最需要解决的问题是什么? 虽然物联网这个行业被炒得比较热,但是截至目前 ...
- apache开源项目--Apache POI
Apache POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.目前POI已经有了Ruby版本. 结构: HSSF - 提供读写Microsoft Excel XLS格式 ...
- tomcat 服务器全解
①B/S.C/S比较 ⑴C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 需要编写服务器端程序,以及客户端程序,例如我们安装的就是QQ的客户端程序: 缺点:软件更新时需要同 ...
- Java笔记(二十四)……集合工具类Collections&Arrays
Collections 集合框架的工具类,方法全部为静态 Collections与Collection的区别 Collection是集合框架的一个顶层接口,里面定义了单列集合的共性方法 Collect ...
- NOIP2015 子串 (DP+优化)
子串 (substring.cpp/c/pas) [问题描述] 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个 互不重 叠 的非空子串,然后把这 k 个子串按照其在字 ...
- sed命令教程
转载自:http://coolshell.cn/articles/9104.htmlawk于1977年出生,今年36岁本命年,sed比awk大2-3岁,awk就像林妹妹,sed就是宝玉哥哥了.所以 林 ...