滑雪(经典DP思想)
个人心得:思想还是不够,开始自己写但是不知道如何记录长度,也不太知道状态的转移,后面看了百度, 发现人人为我我为人人就是一步一步推导,
而递归思想就要求学会记录和找到边界条件,这一题中的话就是用递归,记录他四个方向中最大的那个,然后此时的状态再加一就可以了。
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
Output
Sample Input
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Sample Output
25
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
const int maxn=;
int n,m;
int skating[][];
int ways[][];
int direct[][]={-,,,,,,,-};
int dp(int i,int j)
{
if(ways[i][j]!=-) return ways[i][j];
int maxa=,s;
for(int p=;p<;p++)
{
int nexti=i+direct[p][],nextj=j+direct[p][];
if(nexti>&&nextj>&&nexti<=n&&nextj<=m&&skating[nexti][nextj]<skating[i][j])
{
s=dp(nexti,nextj);
if(maxa<s) maxa=s;
} }
ways[i][j]=maxa+;
return ways[i][j]; }
int main()
{
while(cin>>n>>m){
int flag=-;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
cin>>skating[i][j];
ways[i][j]=-;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(flag<dp(i,j))
flag=dp(i,j); }
cout<<flag<<endl;
}
return ;
}
滑雪(经典DP思想)的更多相关文章
- 到底什么是dp思想(内含大量经典例题,附带详细解析)
期末了,通过写博客的方式复习一下dp,把自己理解的dp思想通过样例全部说出来 说说我所理解的dp思想 dp一般用于解决多阶段决策问题,即每个阶段都要做一个决策,全部的决策是一个决策序列,要你求一个 最 ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- poj1458 求最长公共子序列 经典DP
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45763 Accepted: 18 ...
- NYOJ - 矩形嵌套(经典dp)
矩形嵌套时间限制:3000 ms | 内存限制:65535 KB 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b< ...
- 51nod 1412 AVL树的种类(经典dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1412 题意: 思路: 经典dp!!!可惜我想不到!! $dp[i][k] ...
- NYOJ 16 矩形嵌套(经典DP)
http://acm.nyist.net/JudgeOnline/problem.php?pid=16 矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度: ...
- poj 1050 To the Max 最大子矩阵和 经典dp
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)
Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- CS Academy Distinct Neighbours(经典dp)
CS Academy Distinct Neighbours(经典dp) 题意: 求相邻无相同数字的合法的排列数 题解: 题解 先将相同的数字分为一类,假设共有n组 定义\(dp[i][j]\)表示前 ...
随机推荐
- PAT 天梯赛 L1-028. 判断素数 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-028 AC代码 #include <iostream> #include <cstdio&g ...
- win32调试——OutputDebugString
win32下开发console程序可以直接用printf打印到控制台. 开发图形界面程序时,可以调用OutputDebugString将字符串输出到Debug窗口, 注意是要调试运行才能看到Debug ...
- 去重除了indexOf的其他方法(使用对象Key的方法)及统计重复次数
1.去重: 法1:使用数组IndexOf去重 法2:使用对象Key: <script> var arr1 = [1,13,24,11,11,14,1,2]; let unique = fu ...
- 修改subline text3左侧样式
安装PackageResourceViewer 快捷键 ⌘(command)+⇧(shift)+P 打开 Command Palette 输入 Package Control:Install 回车,等 ...
- 【leetcode刷题笔记】Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- EGLImage与纹理
http://blog.csdn.net/sunnytina/article/details/51895406 Android使用Direct Textures提高glReadPixels.glTex ...
- leetcode刷题3.无重复字符的最长子串
题目:给定一个字符串,找出不含有重复字符的 最长子串 的长度. 示例: 给定 "abcabcbb" ,没有重复字符的最长子串是 "abc" ,那么长度就是3. ...
- asp.net 5 (mvc 6) 获取网站的物理路径
public static IApplicationEnvironment GetApplication(this RazorPage page) { var ae = page.Context.Re ...
- 保护SSH的三把锁
///////////////////////////////写在前面//////////////////////////////////////原帖地址:http://www.ibm.com/dev ...
- java:内存处理ByteArrayOutputStream,ByteArrayInputStream
//用内存,将小写字母替换成大写字母 String str = "helloworld,goodmorning"; ByteArrayOutputStream bos = null ...