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 ...
随机推荐
- C# 页面抓取类
抓取网站页面的内容,简单的类应用,代码如下: /// <summary> /// 获取页面内容 /// </summary> /// <param name=" ...
- C/C++ 内存管理 (《高质量C++》-- 整理笔记)
内存管理是我们在编程时经常遇到的问题,而关于内存管理的问题往往会导致我们无从下手,这篇随笔是我阅读<高质量C++>第7章“内存管理”时一些总结. 1.内存分配方式 在C++中内存分为5个区 ...
- java遍历Map的几种方式
1.遍历map的几种方式:private Hashtable<String, String> emails = new Hashtable<String, String>(); ...
- Linux分区
硬盘分区主要分为基本分区和扩展分区两种,基本分区和扩展分区的数目之和不能大于四个.且基本分区可以马上被使用但不能再分区.扩展分区必须再进行分区后才能进行使用,也就是说它必须进行二次分区.扩展分区再分下 ...
- esp和ebp详解
最近在研究栈帧的结构,但总是有点乱,所以写了一个小程序来看看esp和ebp在栈帧中的作用.这个程序如下: 这个程序很简单,就是求两个数的值,然后输出即可.所以首先把它用gcc编译链接成a.out,进入 ...
- poj 2887 Big String
题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do ...
- c++大数模板
自己写的大数模板,参考了小白书上的写法,只是实现了加减乘法,不支持负数,浮点数.. 除法还没写o(╯□╰)o以后再慢慢更吧.. 其实除法我用(xie)的(bu)少(lai),乘法写过fft,这模板还是 ...
- golang的++与--
http://godoc.golangtc.com/doc/faq#inc_dec 简单地说, 在golang中++,--操作是语句而不是表达式. 所以a=b++, return x++之类绝对提示错 ...
- “后PC”时代来临
“后PC”时代来临 数年前,喜达屋酒店及度假村国际集团将总部搬迁至美国康涅狄格州斯坦福,这也让公司首席执行官Frits van Paasschen有机会“除尘换新”. 那么,Frits van Paa ...
- 基于.net mvc的校友录(五、web.config对的配置以及filter实现的权限控制)
web.config配置文件 此文件是整个系统的配置中心,它告诉iis服务器本网站需要哪些运行时环境,需要哪些环境,将要进行哪些操作,开发人员也会将一个常量性的数据放在此配置中,以备系统全局调用.此文 ...