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 ...
 
随机推荐
- ubuntun pptpd
			
apt-get install pptpd 3.编辑pptpd.conf文件 vi /etc/pptpd.conf 取消注释下面内容 option /etc/ppp/pptpd-options loc ...
 - 有关GIT
			
今天上班,发现没什么事情. 就看了一些博客,发现有个不错的东西,分享一下. 参考:http://www.liaoxuefeng.com/wiki/0013739516305929606dd183612 ...
 - Windows Phone 8.1SDK新特性预览
			
前言 Windows Phone 8.1的预览版将在近期推送,WP 8.1的SDK也已经进入到RC阶段,可以从这里安装.本次更新的SDK被直接集成到了VS2013Update2里面,不再是单独的 ...
 - Learning note for Binding and validation
			
Summary of my learning note for WPF Binding Binding to DataSet. when we want to add new record, we s ...
 - Android实现SharePreferences和AutoCompletedTextView
			
Android实现SharePreferences和AutoCompletedTextView 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 ...
 - Team Homework #3 软件工程在北航——IloveSE
			
任务要求: 采访以前上过北航 (计算机系/软件学院) 软件工程课的同学.现在上研/工作的也可以. 采访问题如下:* 平均每周花在这门课上的时间 (包括上课/作业/上机) * 平均写的代码总行数 ...
 - Careercup - Google面试题 - 5898529851572224
			
2014-05-06 07:56 题目链接 原题: Flatten an iterator of iterators ,], [,[,]], ], it should ,,,,,]. Implemen ...
 - Web前端一种动态样式语言-- Less
			
变量 变量允许我们单独定义一系列通用的样式,然后在需要的时候去调用.所以在做全局样式调整的时候我们可能只需要修改几行代码就可以了. // LESS @color: #4D926F; #header { ...
 - Python编码设置
			
系统编码顺序: 1, a.encode(sys.stdout.encoding) 2, a.encode(default_string) sys.stdout.encoding里的值可以用环境变量PY ...
 - appium for windows 环境搭建
			
服务环境: 1 安装Nodejs 下载nodejs安装包(http://nodejs.org/download/)安装 测试安装是否成功:运行cmd,输入node -v 2 安装android的SKD ...