大致题意:

Description

难怪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更长。事实上,这是最长的一条。

Input

输入的第一行表示区域的行数R和列数C ( 1 ≤ R, C ≤ 100 )

接下来有R行,每行有C个整数,代表高度h,0 ≤ h ≤ 10000

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

记忆化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的更多相关文章

  1. Leetcode 329.矩阵中的最长递增路径

    矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: 输入: n ...

  2. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

  3. Java实现 LeetCode 329 矩阵中的最长递增路径

    329. 矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例 1: ...

  4. [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 ...

  5. [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 ...

  6. LeetCode. 矩阵中的最长递增路径

    题目要求: 给定一个整数矩阵,找出最长递增路径的长度. 对于每个单元格,你可以往上,下,左,右四个方向移动. 你不能在对角线方向上移动或移动到边界外(即不允许环绕). 示例: 输入: nums = [ ...

  7. 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 ...

  8. 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 以矩阵中每一个点作为正方形右下角点来处理,而以该点为右下角点的最大边长最多比 ...

  9. UVA 103 Stacking Boxes (dp + DAG上的最长路径 + 记忆化搜索)

     Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simple in one or t ...

随机推荐

  1. PHP headers_list() 函数

    定义和用法 headers_list() 函数返回已发送的(或待发送的)响应头部的一个列表. 该函数返回包含报头的数组. 语法 headers_list() 提示和注释 提示:如需确定是否已发送报头, ...

  2. BZOJ 4517: [Sdoi2016]排列计数(组合数学)

    题面 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m ...

  3. python中os模块获取路径的几种方式

    一.代码 import os BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) p ...

  4. NX二次开发-UFUN获取图层的状态UF_LAYER_ask_status

    NX11+VS2013 #include <uf.h> #include <uf_ui.h> #include <uf_layer.h> UF_initialize ...

  5. mac 安装并使用 mysql 或者 mac mysql 忘记密码,Can't connect to local MySQL server through socket homebrew

    1. brew install mysql 2. 启动mysql mysql.server start 我遇到了这个error,查openstack解决,我在这粘一下 ### Error:Can't ...

  6. 可搭建SS服务上网的不限流量VPS推荐

    https://itldc.com/en,7个机房,推荐指数:★★★★ 1995年运作至今,有多个机房,包括:新加坡.洛杉矶.新泽西.立陶宛.乌克兰.保加利亚.荷兰.VPS特征: KVM虚拟(支持BB ...

  7. [kuangbin带你飞]专题一 简单搜索 - L - Oil Deposits

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  8. solr +zookeeper+tomcat 集群搭建

    最近需要搭建一个cloudSolr集群,写下记录.基础环境是在centos6.5 64bit 3个下载地址: 1. 下载Solr-4.x http://lucene.apache.org/solr/d ...

  9. 18-Ubuntu-文件和目录命令-创建文件和目录-touch和mkdir

    1.touch 创建文件或修改文件时间 (1)如果文件不存在,可以创建一个空白文件 例: 创建空白文件01.txt touch 01.txt (2)如果文件已经存在,可以修改文件的末次修改时间 例: ...

  10. CentOS7中下载MySQL

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~CentOS 7中,yum默认安装的是Mariadb,但我想使用MyS ...