滑雪 矩阵中的最长上升路径 /// 记忆化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 ...
随机推荐
- splay 模板 洛谷3369
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入 xx 数 删除 xx 数(若有多个相同的数,因只删除一个) 查询 xx 数的排名(排名定义为比当前数小的数 ...
- NX二次开发-UFUN设置工程图PNG图片长度UF_DRF_set_image_width
#include <uf.h> #include <uf_drf.h> UF_initialize(); //插入PNG char* file_name = "D:\ ...
- IOS自动化打包介绍
IOS自动化打包介绍 标签: app打包 , Ios打包 , iphone打包 , iphone自动化打渠道包 分类:无线客户端技术, 贴吧技术 摘要 随着苹果手持设备用户的不断增加,ios应 ...
- STM32F427VI 电流
- Function Run Fun-递归+细节处理
We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a < ...
- Windows环境下安装openface
由于昨天在学习人脸识别,就涉及到了openface 我使用的是Windows环境下的pycharm开发工具,昨天一直安装openface但就是没有相关的教程,使用pip install openfac ...
- mysql 实现批量导入,并解决中文乱码问题
public static String url = "jdbc:mysql://ip/database?characterEncoding=UTF-8"; //在database ...
- SparkListener监听使用方式及自定义的事件处理动作
本文针对spark 2.0+版本 概述 spark 提供了一系列整个任务生命周期中各个阶段变化的事件监听机制,通过这一机制可以在任务的各个阶段做一些自定义的各种动作.SparkListener便是这些 ...
- spark-sql createOrReplaceTempView 和createGlobalTempView区别
在讲解 createOrReplaceTempView 和createGlobalTempView的区别前,先了解下Spark Application 和 Spark Session区别 Spark ...
- Appium 环境配置遇到的坑
一般基础的python路径,sdk等网上都有教程,在这里不多说. 一般可能没有的包:opencv4nodejs ,ffmpeg,bundletoo,jar 1.opencv4nodejs 使用npm安 ...