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

  1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5
6 using namespace std;
7
8 typedef long long ll;
9
10 int N, S;
11 ll dp[50][50][505];
12 char p[50][50][505];
13 int mar[50][50];
14 bool vis[50][50];
15 ll ans = 0;
16
17 void f(int x, int y) {
18 vis[x][y] = 1;
19 int v = x >= N ? 1 : 0;
20 if(mar[x][y] == -1) return;
21 if(!vis[x + 1][y - v]) {
22 f(x + 1,y - v);
23 }
24 for(int i = 0; i <= 500; ++i) {
25 if(dp[x + 1][y - v][i] != 0) {
26 dp[x][y][ mar[x][y] + i] += dp[x + 1][y - v][i];
27 p[x][y][mar[x][y] + i] = ‘L‘;
28 }
29 }
30
31 if(!vis[x + 1][y + 1 - v]) {
32 f(x + 1,y + 1 - v);
33 }
34
35 for(int i = 0; i <= 500; ++i) {
36 if(dp[x + 1][y + 1 - v][i] != 0) {
37 dp[x][y][ mar[x][y] + i] += dp[x + 1][y + 1 - v][i];
38 if(!p[x][y][mar[x][y] + i])
39 p[x][y][mar[x][y] + i] = ‘R‘;
40 }
41 }
42 }
43
44 void output(int x) {
45 printf("%d ", x - 1);
46 for(int i = 1, t = x,nowsum = S; i <= 2 * N - 2; ++i) {
47 printf("%c", p[i][t][nowsum]);
48 int v = i >= N ? 1 : 0;
49 if(p[i][t][nowsum] == ‘L‘) {
50 nowsum -= mar[i][t];
51 t -= v;
52 } else {
53 nowsum -= mar[i][t];
54 t += 1 - v;
55 }
56 }
57 }
58
59 void solve() {
60 memset(vis, 0, sizeof(vis));
61 memset(dp, 0, sizeof(dp));
62 memset(p, 0, sizeof(p));
63
64 ans = 0;
65 for(int i = 1; i <= N; ++i) {
66 dp[2 * N - 1][i][mar[2 * N - 1][i]] = 1;
67 }
68
69 int tar = 0;
70 for(int i = N; i >= 1; --i) {
71 f(1, i);
72 if(dp[1][i][S] != 0) {
73 tar = i;
74 }
75 ans += dp[1][i][S];
76 }
77
78 printf("%lld\n", ans);
79 if(ans != 0) {
80 output(tar);
81 }
82
83 printf("\n");
84 }
85 int main()
86 {
87 freopen("sw.in", "r", stdin);
88 while(~scanf("%d%d", &N, &S) && (N + S)) {
89 memset(mar, -1, sizeof(mar));
90 for(int i = 1; i <= N; ++i) {
91 for(int j = i; j <= N; ++j) {
92 scanf("%d", &mar[i][j]);
93 }
94 }
95
96 for(int i = N + 1; i <= 2 * N - 1; ++i) {
97 for(int j = N - (i - N); j <= N; ++j) {
98 scanf("%d", &mar[i][j]);
99 }
100 }
101 solve();
102 }
103 return 0;
104 }

时间: 2024-08-01 22:39:55

uva 10564的相关文章

UVA 10564 Paths through the Hourglass[DP 打印]

UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径. f[i][j][k]从下往上到第i层第j个和为k的方案数 上下转移不一样,分开处理 没必要判断走出沙漏 打印方案倒着找下去行了,尽量往左走 沙茶的忘注释掉文件WA好多次 #include <iostream> #include <cstdio> #include <algor

uva 10564 Paths through the Hourglass (DP)

uva 10564 Paths through the Hourglass 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

UVA 10564 十 Paths through the Hourglass

Paths through the Hourglass Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10564 1 #include <stdio.h> 2 #include <string.h> 3 4 int n,s; 5 long long dp[55][56][550]; 6 int a[56][55]; 7 8 i

01背包(类) UVA 10564 Paths through the Hourglass

题目传送门 1 /* 2 01背包(类):dp[i][j][k] 表示从(i, j)出发的和为k的方案数,那么cnt = sum (dp[1][i][s]) 3 状态转移方程:dp[i][j][k] = dp[i+1][j][k-c] + dp[i+1][j+1][k-c];(下半部分) 上半部分类似 4 因为要输出字典序最小的,打印路径时先考虑L 5 */ 6 /************************************************ 7 * Author :Runni

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] 方案O(2^2*n-1),结果要用long long保存. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 22,maxs = 501; int hg[maxn<<1][

UVa 10564 - Paths through the Hourglass(DP)

Description 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 pat

UVa 10564 DP Paths through the Hourglass

从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int n, sum; 7 int a[50][25]; 8 long long d[50][50][550]; 9 int p[50][50][550][2];

小白书关于动态规划

10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=show_problem&category=114&problem=1133&mosmsg= Submission+received+with+ID+13297616 */ #include <cstdio> #include <string.h> #include&

训练指南DP阶段训练1

最近又忙又颓.............时间抓不紧....下学期开始就要准备考研了.......就2个月左右可以做自己喜欢的事了....争取把紫书和白书没做的,做过的..来一次完整的总结 训练指南上面的5个例题+后面15个习题是第一阶段 vjudge训练地址 http://vjudge.net/contest/139533#overview -------------------------------------------------------------------------------