uva 10564
Problem F
Paths through the Hourglass
Input: Standard Input
Output: Standard Output
Time Limit: 2 Seconds
In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each cell in the path (except the first) should be directly below to the left or right of the cell in the path in the previous row. The value of a path is the sum of the values in each cell in the path.
A path is described with an integer representing the starting point in the first row (the leftmost cell being 0) followed by a direction string containing the letters L and R, telling whether to go to the left or right. For instance, the path to the right is described as 2 RRRLLRRRLR.
Given the values of each cell in an hourglass as well as an integer S, calculate the number of distinct paths with value S. If at least one pathexist, you should also print the path with the lowest starting point. If several such paths exist, select the one which has the lexicographically smallest direction string.
Input
The input contains several cases. Each case starts with a line containing two integers N and S (2≤N≤20, 0≤S<500), the number of cells in the first row of the hourglass and the desired sum. Next follows 2N-1 lines describing each row in the hourglass. Each line contains a space separated list of integers between 0 and 9 inclusive. The first of these lines will contain N integers, then N-1, ..., 2, 1, 2, ..., N-1, N.
The input will terminate with N=S=0. This case should not be processed. There will be less than 30 cases in the input.
Output
For each case, first output the number of distinct paths. If at least one path exist, output on the next line the description of the path mentioned above. If no path exist, output a blank line instead.
Sample Input Output for Sample Input
6 41 6 7 2 3 6 8 1 8 0 7 1 2 6 5 7 3 1 0 7 6 8 8 8 6 5 3 9 5 9 5 6 4 4 1 3 2 6 9 4 3 8 2 7 3 1 2 3 5 5 26 2 8 7 2 5 3 6 0 2 1 3 4 2 5 3 7 2 2 9 3 1 0 4 4 4 8 7 2 3 0 0 |
1 2 RRRLLRRRLR 0
5 2 RLLRRRLR
|
Problemsetter: Jimmy Mårdell, Member of Elite Problemsetters' Panel
dp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long ll; int N, S;
ll dp[][][];
char p[][][];
int mar[][];
bool vis[][];
ll ans = ; void f(int x, int y) {
vis[x][y] = ;
int v = x >= N ? : ;
if(mar[x][y] == -) return;
if(!vis[x + ][y - v]) {
f(x + ,y - v);
}
for(int i = ; i <= ; ++i) {
if(dp[x + ][y - v][i] != ) {
dp[x][y][ mar[x][y] + i] += dp[x + ][y - v][i];
p[x][y][mar[x][y] + i] = 'L';
}
} if(!vis[x + ][y + - v]) {
f(x + ,y + - v);
} for(int i = ; i <= ; ++i) {
if(dp[x + ][y + - v][i] != ) {
dp[x][y][ mar[x][y] + i] += dp[x + ][y + - v][i];
if(!p[x][y][mar[x][y] + i])
p[x][y][mar[x][y] + i] = 'R';
}
}
} void output(int x) {
printf("%d ", x - );
for(int i = , t = x,nowsum = S; i <= * N - ; ++i) {
printf("%c", p[i][t][nowsum]);
int v = i >= N ? : ;
if(p[i][t][nowsum] == 'L') {
nowsum -= mar[i][t];
t -= v;
} else {
nowsum -= mar[i][t];
t += - v;
}
}
} void solve() {
memset(vis, , sizeof(vis));
memset(dp, , sizeof(dp));
memset(p, , sizeof(p)); ans = ;
for(int i = ; i <= N; ++i) {
dp[ * N - ][i][mar[ * N - ][i]] = ;
} int tar = ;
for(int i = N; i >= ; --i) {
f(, i);
if(dp[][i][S] != ) {
tar = i;
}
ans += dp[][i][S];
} printf("%lld\n", ans);
if(ans != ) {
output(tar);
} printf("\n");
}
int main()
{
freopen("sw.in", "r", stdin);
while(~scanf("%d%d", &N, &S) && (N + S)) {
memset(mar, -, sizeof(mar));
for(int i = ; i <= N; ++i) {
for(int j = i; j <= N; ++j) {
scanf("%d", &mar[i][j]);
}
} for(int i = N + ; i <= * N - ; ++i) {
for(int j = N - (i - N); j <= N; ++j) {
scanf("%d", &mar[i][j]);
}
}
solve();
}
return ;
}
uva 10564的更多相关文章
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 10564 十 Paths through the Hourglass
Paths through the Hourglass Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- 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)
本文出自 http://blog.csdn.net/shuangde800 题目传送门 题意: 给一个相上面的图.要求从第一层走到最下面一层,只能往左下或右下走,经过的数字之和为sum. 问有多少 ...
- 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 DP Paths through the Hourglass
从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. #include <cstdio> #include &l ...
- UVA - 10564 Paths through the Hourglass
传送门:https://vjudge.net/problem/UVA-10564 题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前 ...
- UVA 10564 计数DP
也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞...不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下, ...
- Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) Volume 5. Dynamic Programming
10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=sho ...
随机推荐
- angularjs2 学习笔记(二) 组件
angular2 组件 首先了解angular2 组件的含义 angular2的应用就是一系列组件的集合 我们需要创建可复用的组件供多个组件重复使用 组件是嵌套的,实际应用中组件是相互嵌套使用的 组件 ...
- 银行卡BIN码大全
BIN号即银行标识代码的英文缩写.BIN由6位数字表示,出现在卡号的前6位,由国际标准化组织(ISO)分配给各从事跨行转接交换的银行卡组织.银行卡的卡号是标识发卡机构和持卡人信息的号码,由以下三部分组 ...
- 苏泊尔借助微软CRM提升客户满意度
企业背景 作为中国最大.全球第二的炊具研发制造商和中国小家电领先品牌,品质和创新一是苏泊尔矢志追求的企业理念,从火红点无油烟锅的发明到能做柴火饭的球釜IH饭煲的面世,苏泊尔用产品的创新和品质的承诺,不 ...
- oracle 修改表空间存储路径
[root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 Oracle Database 11g Enterpris ...
- jQuery无缝滚动插件
插件代码 ;(function ($) { // jQuery marquee 插件 $.fn.marquee = function (options) { // 默认设置 var defaults ...
- Git操作指南(2) —— Git Gui for Windows的建库、克隆、上传
本教程将讲述:gitk的Git Gui的部分常用功能和使用方法,包括:建库.克隆(clone).上传(push).下载(pull - fetch).合并(pull - merge). ———————— ...
- [转]insmod
[转]insmod http://www.cnblogs.com/amaoxiaozhu/archive/2013/03/08/2950002.html 在Linux下,驱动程序是内核的一部分,运行在 ...
- [rsync+inotify]——监控客户端文件变化,rsync同步到服务器
关于rsync的配置请参考博文:http://www.cnblogs.com/snsdzjlz320/p/5630695.html 实验环境 (1) Rsync服务器:10.0.10.158 (2) ...
- c++中头文件include规则浅析[译]
英文原文地址 在开发大型的软件项目时,头文件需要得到恰当的管理,甚至在c中也会面临这种问题,当我们用c++开发时,头文件的管理会变得更复杂,更加耗费我们的时间去管理,下面我将讲一些包含规则来简化这个苦 ...
- Teamwork——Week 4 Daily Scrum Meeting#1 2013.10.23
一.会议议题 1)根据确立的项目题目,进一步明确PM,DEV,TEST的工作. 2)确定团队分工和预估项目时间. 3)完成项目架构NABC模型. 4)确定第一轮开发团队分工 二.会议时间 2013年1 ...