UVa 10564 - Paths through the Hourglass (简单DP)
2014-08-11 10:57
387 查看
Paths through the Hourglass
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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 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
[Submit] [Go Back] [Status]题意:
有一个沙漏,如图,从上到下走,每次只能走到下面的相邻格子,问走到最下面的时候累加和Si==所给的S的方案数,如果有多条路径,找出起点尽量小的前提下移动序列(LR)的字典序最小
思路:
DP
不过要求字典序最小 所以我用string来保存了路径
范围会超出int
/************************************************************************* > Name: ./10564.cpp > Created Time: 2014年08月11日 星期一 08时26分18秒 ************************************************************************/ #include <cstdio> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #include <string> #include <map> #include <cmath> #include <queue> #include <set> using namespace std; //#define WIN #ifdef WIN typedef __int64 LL; #define iform "%I64d" #define oform "%I64d\n" #define oform1 "%I64d" #else typedef long long LL; #define iform "%lld" #define oform "%lld\n" #define oform1 "%lld" #endif #define S64I(a) scanf(iform, &(a)) #define P64I(a) printf(oform, (a)) #define P64I1(a) printf(oform1, (a)) #define REP(i, n) for(int (i)=0; (i)<n; (i)++) #define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++) #define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++) const int INF = 0x3f3f3f3f; const double eps = 10e-9; const double PI = (4.0*atan(1.0)); const int maxn = 50; const int maxS = 500 + 10; int A[maxn][maxn]; LL dp[maxn][maxn][maxS]; int start[maxn][maxn][maxS]; string path[maxn][maxn][maxS]; int main() { int n, S; while(scanf("%d%d", &n, &S) != EOF && (n || S)) { for(int i=0; i<n*2-1; i++) { for(int j=0; j<1+abs(n-i-1); j++) { scanf("%d", &A[i][j]); } } memset(dp, 0, sizeof(dp)); for(int i=0; i<n; i++) { dp[0][i][A[0][i]] = 1; start[0][i][A[0][i]] = i; for(int j=0; j<=S; j++) { path[0][i][j] = ""; } } for(int i=1; i<n*2-1; i++) { for(int j=0; j<1+abs(n-i-1); j++) { int p1 = j, p2 = j+1; if(i >= n) p1--, p2--; for(int k=A[i][j]; k<=S; k++) { int & ss = start[i][j][k] = INF; LL & dd = dp[i][j][k] = 0; string & pp = path[i][j][k] = "ZZZ"; if(p1 >= 0 && dp[i-1][p1][k-A[i][j]] > 0) { dd = dp[i-1][p1][k-A[i][j]]; pp = path[i-1][p1][k-A[i][j]] + 'R'; ss = start[i-1][p1][k-A[i][j]]; } if(p2 < 1+abs(n-i) && dp[i-1][p2][k-A[i][j]] > 0) { dd += dp[i-1][p2][k-A[i][j]]; string p = path[i-1][p2][k-A[i][j]] + 'L'; int s = start[i-1][p2][k-A[i][j]]; if(s < ss || (s == ss && p < pp)) { ss = s; pp = p; } } } } } LL num = 0; int s= INF; string ans = "ZZ"; int nn = n * 2 - 2; for(int i=0; i<n; i++) { if(dp[nn][i][S] > 0) { num += dp[nn][i][S]; int ss = start[nn][i][S]; string pp = path[nn][i][S]; if(ss < s || (ss == s && pp < ans)) { s = ss; ans = pp; } } } P64I(num); if(num) { printf("%d %s\n", s, ans.c_str()); } else putchar('\n'); } return 0; }
相关文章推荐
- Paths through the Hourglass UVA - 10564 简单dp (码力)
- UVA10564-----Paths through the Hourglass-----简单的计数DP
- UVa10564 - Paths through the Hourglass(dp)
- UVa 10564 Paths through the Hourglass(DP)
- UVa 10564 DP Paths through the Hourglass
- UVA 10564 - Paths through the Hourglass (dp)
- uva 10564 - Paths through the Hourglass(dp)
- UVa 10564 Paths through the Hourglass (DP)
- UVa 10564 - Paths through the Hourglass(DP)
- UVA 10564 - Paths through the Hourglass (dp)
- UVA 10564 Paths through the Hourglass[DP 打印]
- UVA 10564 - Paths through the Hourglass (dp)
- Paths through the Hourglass - UVa 10564 dp
- uva 10564 Paths through the Hourglass (DP)
- uva 10564 Paths through the Hourglass(DP)
- UVA 10564 Paths through the Hourglass(dp)
- uva 10564 - Paths through the Hourglass(dp)
- UVA - 10564 Paths through the Hourglass DAG
- uva 10564 Paths through the Hourglass
- UVA 10564 Paths through the Hourglass