滑雪 矩阵中的最长上升路径 /// 记忆化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 ...
随机推荐
- Eureka的表兄弟Zookeeper理论基础
Eureka的表兄弟Zookeeper 简单介绍 Zookeeper是一个开源的分布式应用程序协调服务器,主要功能包括配置维护,域名服务,分布式同步,集群管理等 主要功能简介 一.配置维护 分布式系统 ...
- springboot连接mysql数据库,JdbcTemplate和spring JPA方式
SQL部分 CREATE TABLE test( id ) primary key, name ) not null, age ), address ) ); ,,'bj'); ,,'sh'); ,, ...
- php数组函数,遍历数组的几种方法
数组创建: 1.array(): 生成一个数组 $a=array("Dog","Cat","Horse"); print_r($a);数组值 ...
- post请求传文件
public static JSONObject doFormDataPost(File file, String sURL) throws IOException { HttpClient cont ...
- Python的变长参数
Python的变长参数 def foo1(*args): for arg in args: print arg def foo2(**kargs): for key in kargs: print k ...
- (转)VS2010-MFC编程入门教程之目录和总结
目前该教程可以到鸡啄米编程课堂去学习,阅读体验更好,更适合在线学习. 原文目录及链接: 一.VS2010/MFC编程入门教程之目录 第一部分:VS2010/MFC开发环境 VS2010/MFC编程入 ...
- 杂项:ionic
ylbtech-杂项:ionic ionic是一个用来开发混合手机应用的,开源的,免费的代码库.可以优化html.css和js的性能,构建高效的应用程序,而且还可以用于构建Sass和AngularJS ...
- Linux_磁盘分区、挂载、查看
一.挂载 1.查看设备的挂载情况 lsblk或lsblk -f 2.挂载 需求 :给我们的Linux系统增加一个新的硬盘,并且挂载到/home/newdisk 说明:我们以增加一块硬盘为例来熟悉一下磁 ...
- Nutz框架的优点
轻量级 -- jar文件共约1Mb -- 针对JDBC的薄封装,无缓存. 全面 -- 提供了Dao(ORM, SQL管理),Ioc, Aop, Mvc, Json解析等必要功能. 灵活 -- 各个部分 ...
- ActiveMQ 反序列化漏洞(CVE-2015-5254)
java -jar jmet-0.1.0-all.jar -Q event -I ActiveMQ -s -Y "touch /tmp/success" -Yp ROME 192. ...