滑雪 矩阵中的最长上升路径 /// 记忆化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 ...
随机推荐
- QueryList采集页面链接及对应标题
<?php header('content-type:text/html;charset=utf-8'); require 'vendor/autoload.php'; use QL\Query ...
- [转]gulp打包工具总结
与grunt类似,gulp也是构建工具,但相比于grunt的频繁IO操作,gulp的流操作能更快更便捷地完成构建工作.gulp借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级 ...
- idea使用问题
1. 问题: 突发断电导致idea的play项目错误,无法识别build.sbt,build.sbt文件报错,Cannot resolve symbol 解决方案: For anyone having ...
- Linux服务器上监控网络带宽的18个常用命令nload, iftop,iptraf-ng, nethogs, vnstat. nagios,运用Ntop监控网络流量
Linux服务器上监控网络带宽的18个常用命令 本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量 ...
- 在WinDBG中查看内存的命令
当我们在调试器中分析问题时, 经常需要查看不同内存块的内容以分析产生的原因, 并且在随后验证所做出的假设是否正确. 由于各个对象的状态都是保存在内存中的, 因此内存的内容也就相当于对象的状态. d命令 ...
- Oracle 生成sys_guid
select sys_guid() from dual;select sys_guid() from dual connect by rownum<100
- tensorflow 训练的时候loss=nan
出现loss为nan 可能是使用了relu激活函数,导致的.因为在负半轴上输出都是0
- TensorFlow 与cudnn版本不匹配问题
log:Loaded runtime CuDNN library: 7.1.4 but source was compiled with: 7.2.1. 我安装的事cuda 9.0 cudnn 7. ...
- oracle 管理表空间
表空间:是oracle数据库中最大的逻辑存储结构,与操作系统中的数据文件相对应,用于存储数据库中用户创建的所有内容 表空间>数据文件 4.1创建一个永久性表空间myspace create ta ...
- 2.用Python套用Excel模板,一键完成原亮样式
from xlutils.copy import copy import xlrd import xlwt tem_excel=xlrd.open_workbook('日统计.xls',formatt ...