UVA - 10564 Paths through the Hourglass
传送门:https://vjudge.net/problem/UVA-10564
题目大意:给你一张形如沙漏一般的图,每一个格子有一个权值,问你有多少种方案可以从第一行走到最后一行,并且输出起点最靠前的方案,以及此方案的起点编号,起点相同则字典序最小。
题解:
很容易想到一个DP,dp[i][j][S]代表到第i层,第j列,从第一层到这里的路径和为S的方案数,最后只要查询最后一行的方案数即可了。但是这样很不好输出方案!我们可能需要从终点向上dfs,但是又无法保证起点编号最小以及字典序最小。但是我们能从终点向上dfs,那么如果我们反过来DP呢?可行啊,这样就可以从起点向终点dfs了,贪心选择可行的路径即可。
需要注意的是上下两个一半的沙漏的转移一个是到j与j+1,一个是j与j-1,要考虑清楚细节!
- #include<queue>
- #include<cstdio>
- #include<vector>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define RG register
- #define LL long long
- #define fre(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
- using namespace std;
- const int MAXN=,MAXS=;
- int n;
- LL ans,S;
- LL a[MAXN*][MAXN],dp[MAXN*][MAXN][MAXS];
- void dfs(int x,int y,int sum);
- int main()
- {
- while(scanf("%d%lld",&n,&S)!=EOF)
- {
- if(n+S==)break;
- for(int i=;i<=n;i++) for(int j=;j<=n-i+;j++) scanf("%lld",&a[i][j]);
- for(int i=n+;i<*n;i++) for(int j=;j<=i-n+;j++) scanf("%lld",&a[i][j]);
- memset(dp,,sizeof dp);
- for(int i=;i<=n;i++) dp[n*-][i][a[n*-][i]]=;
- for(int i=n*-;i>=n;i--)
- for(int j=;j<=i-n+;j++)
- for(int k=;k<=S;k++)
- {
- if(dp[i+][j][k] && k+a[i][j]<=S) dp[i][j][k+a[i][j]]+=dp[i+][j][k];
- if(dp[i+][j+][k] && k+a[i][j]<=S) dp[i][j][k+a[i][j]]+=dp[i+][j+][k];
- }
- for(int i=n-;i>=;i--)
- for(int j=;j<=n-i+;j++)
- for(int k=;k<=S;k++)
- {
- if(dp[i+][j][k] && k+a[i][j]<=S) dp[i][j][k+a[i][j]]+=dp[i+][j][k];
- if(dp[i+][j-][k] && k+a[i][j]<=S) dp[i][j][k+a[i][j]]+=dp[i+][j-][k];
- }
- ans=; for(int i=;i<=n;i++) ans+=dp[][i][S]; printf("%lld\n",ans);
- for(int i=;i<=n;i++)
- if(dp[][i][S])
- {
- printf("%d ",i-);
- int x=,y=i,sum=S;
- while(x<n*-)
- {
- if(x<n)
- {
- if(dp[x+][y-][sum-a[x][y]]) sum-=a[x][y],y--,printf("L");
- else sum-=a[x][y],printf("R");
- }
- else
- {
- if(dp[x+][y][sum-a[x][y]]) sum-=a[x][y],printf("L");
- else sum-=a[x][y],y++,printf("R");
- }
- x++;
- }
- break;
- }
- printf("\n");
- }
- return ;
- }
UVA - 10564 Paths through the Hourglass的更多相关文章
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- 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_ Paths through the Hourglass
题意: 由0-9的数字组成一个形如沙漏的图形,要求从第一行开始沿左下或者右下到达最后一行,问有多少种不同的路径,使最后路径上的整数之和为给定的某个数. 分析: 简单计数dp,从最后一行开始,设dp[i ...
- UVA 10564 十 Paths through the Hourglass
Paths through the Hourglass Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- uva 10564
Problem FPaths through the HourglassInput: Standard Input Output: Standard Output Time Limit: 2 Seco ...
- UVa 10564 DP Paths through the Hourglass
从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. #include <cstdio> #include &l ...
- UVA 10564 计数DP
也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞...不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下, ...
随机推荐
- POJ3067:Japan(线段树)
Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for th ...
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 使用Napa开发SharePoint应用程序
BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 使用Napa开发SharePoint应用程序 假设 ...
- SQL模糊查询碰到空值怎么办?
作者:iamlaosong SQL查询语句用%来做模糊查询.程序中一般要求用户输入部分信息,依据这个信息进行模糊查询. 比如用户输入340104,以下这条语句就是查询昨天客户代码为340104开头的全 ...
- HDU 1312 Red and Black 第一题搜索!
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- nyoj 题目10 skiing —— 南阳oj
题目信息例如以下: skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激.但是为了获得速度,滑的区 ...
- LeetCode题解(13)--Roman to Integer
https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...
- How MySQL Opens and Closes Tables refuse connections 拒绝连接的原因 file descriptors
MySQL :: MySQL 5.7 Reference Manual :: 8.4.3.1 How MySQL Opens and Closes Tables https://dev.mysql.c ...
- 在Android Studio中移除导入的模块依赖
进入settings.gradle(Project Settings) include ':app', ':pull_down_list_view' 要移除的Module dependency为“pu ...
- Ural 1635 Mnemonics and Palindromes(DP)
题目地址:space=1&num=1635">Ural 1635 又是输出路径的DP...连着做了好多个了. . 状态转移还是挺简单的.要先预处理出来全部的回文串,tag[i] ...
- 百度dureos CMake Error
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage CMake Error: CMAKE_CXX_COMPILER not set, ...