滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919
大致题意:
难怪Michael喜欢滑雪,因为滑雪确实很刺激。为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道在一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
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更长。事实上,这是最长的一条。
输入的第一行表示区域的行数R和列数C ( 1 ≤ R, C ≤ 100 )
接下来有R行,每行有C个整数,代表高度h,0 ≤ h ≤ 10000
输出最长倾斜向下的滑道长度。
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
25
记忆化DFS 时间限制100ms内
#include<bits/stdc++.h>
using namespace std;
int G[][],len[][],have[][];
/// len[][]为当前点的最长路, have[][]为当前点已搜过具有最长路len[][]
int n,m,mov[][]={,,,-,,-,,};
bool bound(int p,int q) // 越界
{
return p>n||p<||q>m||q<;
}
int DFS(int p,int q)
{
if(have[p][q]) return len[p][q]; //已搜过的直接返回其最长路
int ans=;
for(int i=;i<;i++)
{
int np=p+mov[][i],nq=q+mov[][i];
if(bound(np,nq)) continue;
if(G[p][q]>G[np][nq]) ans=max(ans,DFS(np,nq)+);
} /// 搜该点出发的四个点 取其中路最长的一个值+1
len[p][q]=ans; /// 存入该点的最长路
have[p][q]=; /// 搜完四个点 则该点最长路已更新 标为已搜过
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&G[i][j]);
memset(have,,sizeof(have));
int ans=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
len[i][j]=DFS(i,j); /// 深搜该点 得到其最长路
ans=max(ans,len[i][j]);
}
printf("%d",ans); return ;
}
DP 时间限制1000ms内
#include<bits/stdc++.h>
using namespace std;
int n,c,dp[*];
struct NODE{ int x,y,l; }node[*];
bool cmp(NODE a,NODE b){ return a.l>b.l; }
bool cheak(int i,int j)
{
int a=fabs(node[i].x-node[j].x);
int b=fabs(node[i].y-node[j].y);
if(a+b==) return ;
return ;
}
int main()
{
scanf("%d%d",&n,&c);
int len=;
for(int i=;i<=n;i++)
for(int j=;j<=c;j++)
{
int m; scanf("%d",&m);
node[len].x=i, node[len].y=j;
node[len++].l=m;
}
sort(node,node+len,cmp);
memset(dp,,sizeof(dp));
int ans=;
for(int i=;i<len;i++)
{
for(int j=;j<i;j++)
if(cheak(i,j)&&node[j].l>node[i].l)
dp[i]=max(dp[i],dp[j]+);
ans=max(dp[i],ans);
}
printf("%d",ans+); return ;
}
滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919的更多相关文章
- Leetcode 329.矩阵中的最长递增路径
矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...
- Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)
Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...
- Java实现 LeetCode 329 矩阵中的最长递增路径
329. 矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: ...
- [LeetCode] Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- [Swift]LeetCode329. 矩阵中的最长递增路径 | Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- LeetCode. 矩阵中的最长递增路径
题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ ...
- 329 Longest Increasing Path in a Matrix 矩阵中的最长递增路径
Given an integer matrix, find the length of the longest increasing path.From each cell, you can eith ...
- 01二维矩阵中最大全为1的正方形maxSquare——经典DP问题(二维)
在一个二维01矩阵中找到全为1的最大正方形 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 以矩阵中每一个点作为正方形右下角点来处理,而以该点为右下角点的最大边长最多比 ...
- UVA 103 Stacking Boxes (dp + DAG上的最长路径 + 记忆化搜索)
Stacking Boxes Background Some concepts in Mathematics and Computer Science are simple in one or t ...
随机推荐
- JAVA Java中@Override的作用
@Override是伪代码,表示重写(当然不写也可以),不过写上有如下好处: 1.可以当注释用,方便阅读: 2.编译器可以给你验证@Override下面的方法名是否是你父类中所有的,如果没有则报错.例 ...
- 2019.7.3模拟 七星连珠(行列式+随机+k进制FWT)
题目大意: 给一个\(n*n\)的矩阵,对于所有排列p,记录\(a[i][p[i]]\)的k进制下不进位加法的结果,问所有被记录过的数. \(n<=50,p=2.3,0<=a[i][j]& ...
- noip 2014 总结
2014 年的noip 已经结束了,成绩从个人而言不是特别的理想,从今年题的逗的程度,本来是个xxx 就被玩脱了= = 当然现在后悔事没有用的了,不过第二天全屏技术的在最后一小时看到了两道题的错误,然 ...
- NX二次开发-创建图纸尺寸表达式抑制UF_DRF_add_controlling_exp
#include <uf.h> #include <uf_modl.h> #include <uf_drf.h> #include <uf_obj.h> ...
- 原生js - 两种图片懒加载实现原理
目前图片懒加载的方式主要有两种: 1.利用getBoundingClientRectAPI得到当前元素与视窗的距离来判断 2.利用h5的新API IntersectionObserver 来实现 ge ...
- px2rem-loader(Vue:将px转化为rem,适配移动端)
转载:https://www.cnblogs.com/WQLong/p/7798822.html 1.下载lib-flexible 使用的是vue-cli+webpack,通过npm来安装的 npm ...
- 实战CGLib系列之proxy篇(一):方法拦截MethodInterceptor
实战CGLib系列文章 本篇介绍通过MethodInterceptor和Enhancer实现一个动态代理. 一.首先说一下JDK中的动态代理: JDK中的动态代理是通过反射类Proxy以及Invoca ...
- Flink 1.9 FlinkKafkaProducer 使用 EXACTLY_ONCE 错误记录
使用flink FlinkKafkaProducer 往kafka写入数据的时候要求使用EXACTLY_ONCE语义 本以为本以为按照官网写一个就完事,但是却报错了 代码 package com.me ...
- PAT_A1043#Is It a Binary Search Tree
Source: PAT A1043 Is It a Binary Search Tree (25 分) Description: A Binary Search Tree (BST) is recur ...
- [14]APUE:API for Mysql
库:/usr/lib64/libmysqlclient.so.#.#... 头文件:/usr/lib64/mysql/mysql.h 一.建立连接 MYSQL *mysql_init(MYSQL *) ...