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 int d(int x,int y,int m)
 9 {
10     if(x>=2*n-1)
11         return 0;
12     int v=a[x][y];
13     if(dp[x+1][y][m-v]>0)
14     {
15         printf("L");
16         d(x+1,y,m-v);
17     }
18     else
19     {
20         printf("R");
21         d(x+1,y+1,m-v);
22     }
23     return 0;
24 }
25
26 int main()
27 {
28     int i,j,k;
29     while(scanf("%d %d",&n,&s)!=EOF)
30     {
31         if(n==0 && s==0)
32             break;
33         memset(dp,0,sizeof(dp));
34         for(i=1;i<=n;i++)
35         {
36             for(j=i;j<=n;j++)
37             {
38                 scanf("%d",&a[i][j]);
39             }
40         }
41         for(i=n+1;i<=2*n-1;i++)
42         {
43             for(j=n;j<=i;j++)
44             {
45                 scanf("%d",&a[i][j]);
46             }
47         }
48
49         for(i=n;i<=2*n-1;i++)
50         {
51             int v=a[2*n-1][i];
52             dp[2*n-1][i][v]=1;
53         }
54         for(i=n*2-2;i>n;i--)
55         {
56             for(j=n;j<=i;j++)
57             {
58                 int v=a[i][j];
59                 for(k=v;k<=s;k++)
60                 {
61                     dp[i][j][k]=dp[i+1][j][k-v]+dp[i+1][j+1][k-v];
62                 }
63             }
64         }
65         for(i=n;i>=1;i--)
66         {
67             for(j=i;j<=n;j++)
68             {
69                 int v=a[i][j];
70                 for(k=v;k<=s;k++)
71                 {
72                     dp[i][j][k]=dp[i+1][j][k-v]+dp[i+1][j+1][k-v];
73                 }
74             }
75         }
76
77         long long cnt=0;
78         for(i=1;i<=n;i++)
79         {
80             cnt=cnt+dp[1][i][s];
81         }
82         printf("%lld\n",cnt);
83         for(i=1;i<=n;i++)
84         {
85             if(dp[1][i][s]>0)
86             {
87                 printf("%d ",i-1);
88                 d(1,i,s);
89                 break;
90             }
91         }
92         printf("\n");
93     }
94     return 0;
95 }

时间: 2024-10-21 07:31:57

UVA 10564 十 Paths through the Hourglass的相关文章

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];

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

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)

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

uva10564 - Paths through the Hourglass(递推)

题目:uva10564 - Paths through the Hourglass(递推) 题目大意:给出这样的两个数塔,然后给出一个值,问你能否从这个数塔中找到路径,路径上的值之和等于这个数,输出这样的路径的总数,如果多条打印路径先挑开始的位置(0..n - 1)最小的,如果这样还是有多条,在比较后面的向左向右字典序最小的. 解题思路:一开始两个数塔一个正着推,一个倒着推,结果数目是出来了,但是路径就难办了,最后把这两个数塔都正着推,只是状态转移方程不一样.这样路径输出就比较好处理. 代码:

uva 10564

Problem FPaths through the HourglassInput: 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

uva 125 Numbering Paths(warshall算法)

uva 125 Numbering Paths Description Download as PDF Background Problems that process input and generate a simple yes'' orno" answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general ef

UVa 988 - Many Paths, One Destination

题目:人生有很多选择,现在给你一些选择(0~n-1),和每个选择分支后面的其他选择序号,求选择总数. 分析:dp,图论.如果某状态的后续选择个数是0个则,代表死亡,统计所有到达死亡的路径条数即可. 用一个状态数组记录到达每个选择的路径数,它等于能到达它的前驱节点的路径加和. 稀疏图,使用邻接表储存.初始是节点0的路径条数为1,代表出生. 说明:没有给数据范围略坑啊,RE一次,WA一次,╮(╯▽╰)╭. #include <iostream> #include <cstdlib> #