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+1][j][k-c] + dp[i+1][j+1][k-c];(下半部分) 上半部分类似
因为要输出字典序最小的,打印路径时先考虑L
*/
/************************************************
* Author :Running_Time
* Created Time :2015-8-9 15:45:11
* File Name :UVA_10564.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = ;
const int MAXS = 5e2 + ;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + ;
int a[MAXN*][MAXN];
ll dp[MAXN*][MAXN][MAXS];
int n, s; void print(int x, int y, int sum) {
if (x >= * n - ) return ;
int v = a[x][y];
if (x < n) {
if (y > && dp[x+][y-][sum-v]) {
printf ("L"); print (x+, y-, sum - v);
}
else {
printf ("R"); print (x+, y, sum - v);
}
}
else {
if (dp[x+][y][sum-v]) {
printf ("L"); print (x+, y, sum - v);
}
else {
printf ("R"); print (x+, y+, sum - v);
}
}
} int main(void) { //UVA 10564 Paths through the Hourglass
while (scanf ("%d%d", &n, &s) == ) {
if (!n && !s) break;
for (int i=; i<=n; ++i) {
for (int j=; j<=n-i+; ++j) scanf ("%d", &a[i][j]);
}
for (int i=n+; i<=*n-; ++i) {
for (int j=; j<=i-n+; ++j) scanf ("%d", &a[i][j]);
} memset (dp, , sizeof (dp));
for (int i=; i<=n; ++i) {
int c = a[*n-][i];
dp[*n-][i][c] = ;
}
for (int i=*n-; i>=n; --i) {
for (int j=; j<=i-n+; ++j) {
int c = a[i][j];
for (int k=c; k<=s; ++k) {
dp[i][j][k] = dp[i+][j][k-c] + dp[i+][j+][k-c];
}
}
}
ll cnt = ;
for (int i=n-; i>=; --i) {
for (int j=; j<=n-i+; ++j) {
int c = a[i][j];
for (int k=c; k<=s; ++k) {
if (j > ) dp[i][j][k] += dp[i+][j-][k-c];
if (j < n - i + ) dp[i][j][k] += dp[i+][j][k-c];
}
if (i == ) cnt += dp[i][j][s];
}
}
printf ("%lld\n", cnt);
for (int i=; i<=n; ++i) {
if (dp[][i][s]) {
printf ("%d ", i-);
print (, i, s); break;
}
}
puts ("");
} return ;
}
01背包(类) UVA 10564 Paths through the Hourglass的更多相关文章
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- 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 (dp)
本文出自 http://blog.csdn.net/shuangde800 题目传送门 题意: 给一个相上面的图.要求从第一层走到最下面一层,只能往左下或右下走,经过的数字之和为sum. 问有多少 ...
- UVA - 10564 Paths through the Hourglass
传送门:https://vjudge.net/problem/UVA-10564 题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前 ...
- UVA 10564_ Paths through the Hourglass
题意: 由0-9的数字组成一个形如沙漏的图形,要求从第一行开始沿左下或者右下到达最后一行,问有多少种不同的路径,使最后路径上的整数之和为给定的某个数. 分析: 简单计数dp,从最后一行开始,设dp[i ...
- JOISC2014 挂饰("01"背包)
传送门: [1]:洛谷 [2]:BZOJ 参考资料: [1]:追忆:往昔 •题解 上述参考资料的讲解清晰易懂,下面谈谈我的理解: 关键语句: 将此题转化为 "01背包" 类问题,关 ...
- Uva 12563,劲歌金曲,01背包
题目链接:https://uva.onlinejudge.org/external/125/12563.pdf 题意:n首歌,每首歌的长度给出,还剩 t 秒钟,由于KTV不会在一首歌没有唱完的情况下切 ...
- UVA 10564 十 Paths through the Hourglass
Paths through the Hourglass Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- UVA 624 - CD (01背包 + 打印物品)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- android studio配置so文件路径
将一个项目从eclipse上移植到android studio时,发现总是加载不成功库文件,so库文件放在了main/src/libs下的目录. 参考网上资料,studio默认的库文件路径是main/ ...
- Count the Colors-ZOJ1610(线段树区间求)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- codevs——1006 等差数列
1006 等差数列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给定n(1<=n< ...
- Android GIS开发系列-- 入门季(7) 利用GeometryEngine坐标转换、计算距离与面积等
GeometryEngine是Arcgis的重要工具类,利用此工具类,可以计算地图上的距离.面积,将点.线.面转化为Json数据,将Json转化为点线面,坐标转换作用非常强大. 一.坐标转化 将用到方 ...
- 我的arcgis培训照片3
来自:http://www.cioiot.com/successview-557-1.html
- 如何探测浏览器是否开启js功能
<body> ... ... <script type="text/javascript"> <!-- document.write("He ...
- CentOS firewall添加开放端口
添加 firewall-cmd --zone=public --add-port=80/tcp --permanent (–permanent永久生效,没有此参数重启后失效) 重新载入 firewal ...
- 再探gdb经常使用命令
前面已经有了一篇对gdb经常使用命令的总结.见 http://blog.csdn.net/u011848617/article/details/12838875 这里对眼下学过的gdb命令进行了 ...
- dotNet Core初学之创建第一个dotNetCore项目
首先创建解决方案dotNetCrazy 一.创建项目 1.这里选择.Net Core 选择ASP.NET Core Web 应用程序 名称暂且叫CoreCrazy 这里我们选择 web应用程序(模型视 ...
- Android 布局自适应屏幕
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenFzNjI3NjExMzA=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...