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个字符能化成最少的回文串 ...
随机推荐
- android:process为耗资源操作指定一个新进程
当有一些耗费内存比较多的操作时,可以通过android:process指定一个新的进程.保证程序运行. 例如: 一个后台长期运行的service: <service android:name=& ...
- C#中的OLEDB连接2
在通过ADO对Excel对象进行连接时(此时Excel则认为是一个数据源),需要配置对Excel数据源对应的连接串,这个连接串中包括了Provider信息(其实类似对数据库进行连接操作时,都需要指定连 ...
- mingw32 下编译 zlib
cp win32/makefile.gcc makefile.gcc make -f makefile.gcc make install -f Makefile.gcc INCLUDE_PATH=/m ...
- 不要直接对Request.Headers["If-Modified-Since"]使用Convert.ToDateTime
不要直接对Request.Headers["If-Modified-Since"]使用Convert.ToDateTime 前一段时间图片处理服务一直报“System.Format ...
- If-Modified-Since & If-None-Match
google告诉网站站长:您的网络服务器支持 If-Modified-Since HTTP 标头.通过该功能,您的网络服务器可以告诉 Google 自上次抓取您的网站以来,内容是否已发生变化.该功能可 ...
- Setting Up the ADT Bundle
Setting Up the ADT Bundle The ADT Bundle provides everything you need to start developing apps, incl ...
- C语言setjmp函数使用
C语言中可以使用goto进行程序跳转,但是goto只能使用在一个函数内部,不能实现在不同函数之间的跳转,C语言因此提供了setjmp和longjmp函数实现此功能,一般进行异常处理. 从函数名称可以看 ...
- 为EF DbContext生成的实体添加注释(T5模板应用)[转]
1 先加上类注释 找到这行代码WriteHeader(codeStringGenerator, fileManager): 在它下面加上我们的代码: string summary=string.Emp ...
- NodeJS 框架 Express 从 3.0升级至4.0的新特性
NodeJS 框架 Express 从 3.0升级至4.0的新特性 [原文地址:√https://scotch.io/bar-talk/expressjs-4-0-new-features-and-u ...
- 洛谷P1294 高手去散步
洛谷1294 高手去散步 题目背景 高手最近谈恋爱了.不过是单相思.“即使是单相思,也是完整的爱情”,高手从未放弃对它的追求.今天,这个阳光明媚的早晨,太阳从西边缓缓升起.于是它找到高手,希望在晨读开 ...