当前位置:网站首页>信息学奥赛一本通 1280:【例9.24】滑雪 | OpenJudge NOI 2.6 90:滑雪 | 洛谷 P1434 [SHOI2002] 滑雪
信息学奥赛一本通 1280:【例9.24】滑雪 | OpenJudge NOI 2.6 90:滑雪 | 洛谷 P1434 [SHOI2002] 滑雪
2022-06-10 16:42:00 【君义_noip】
【题目链接】
ybt 1280:【例9.24】滑雪
OpenJudge NOI 2.6 90:滑雪
洛谷 P1434 [SHOI2002] 滑雪
【题目考点】
1. 动态规划
2. 记忆化递归
【解题思路】
1. 状态定义
集合:所有的路线
限制:从某位置出发的路线
属性:路线长度
条件:最长
统计量:路线长度
状态定义:dp[i][j]:从(i,j)出发的所有路线中,长度最长的路线的长度。
2. 状态转移方程
记第(i,j)位置的高度为a[i][j]。
集合:从(i,j)出发的所有路线
分割集合:根据下一步可以到达的位置分割集合。
下一步可以到达的位置有:(i-1,j), (i+1,j), (i,j-1), (i,j+1)。只要该位置的高度低于当前(i,j)位置的高度,就可以到达这里。
- 如果
a[i-1][j] < a[i][j],那么下一步可以到(i-1,j)。从(i,j)出发的最长路线长度,为从(i-1,j)出发的最长路线长度再加1,即dp[i][j] = dp[i-1][j] + 1。 - 如果
a[i+1][j] < a[i][j],那么下一步可以到(i+1,j)。从(i,j)出发的最长路线长度,为从(i+1,j)出发的最长路线长度再加1,即dp[i][j] = dp[i+1][j] + 1。 - 如果
a[i][j-1] < a[i][j],那么下一步可以到(i,j-1)。从(i,j)出发的最长路线长度,为从(i,j-1)出发的最长路线长度再加1,即dp[i][j] = dp[i][j-1] + 1。 - 如果
a[i][j+1] < a[i][j],那么下一步可以到(i,j+1)。从(i,j)出发的最长路线长度,为从(i,j+1)出发的最长路线长度再加1,即dp[i][j] = dp[i][j+1] + 1。 - 以上四种情况求最大值。
由于在求(i,j)时,其上下左右四个位置的状态:dp[i-1][j], dp[i+1][j], dp[i][j-1], dp[i][j+1]并不能确定都已经求出来了。因此可以通过记忆化递归的方法来求解状态。
设函数dfs(i, j),作用为求解状态dp[i][j]。如果dp[i][j]已经求出来了(值大于0),那么直接返回dp[i][j]。否则通过上述状态转移方程递归求解。
遍历所有的位置,求出从每个位置出发的路线的最长长度,求它们中的最大值,即为该问题的结果。
【题解代码】
解法1:动态规划 记忆化递归
#include<bits/stdc++.h>
using namespace std;
#define N 105
int dp[N][N], a[N][N], mx, r, c;//dp[i][j]:从(i,j)出发的最长路线的长度
int dir[4][2] = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};//方向数组
int dfs(int sx, int sy)//求dp[sx][sy]
{
if(dp[sx][sy] > 0)//如果路径长度大于0,则已经求出过dp[sx][sy],直接返回该值。
return dp[sx][sy];
int route = 0;//从(sx,sy)周围位置出发能得到的最大路线数
for(int i = 0; i < 4; ++i)
{
int x = sx + dir[i][0], y = sy + dir[i][1];
if(x >= 1 && x <= r && y >= 1 && y <= c && a[x][y] > a[sx][sy])//如果
route = max(route, dfs(x, y));
}
return dp[sx][sy] = route+1;
}
int main()
{
cin >> r >> c;
for(int i = 1; i <= r; ++i)
for(int j = 1; j <= c; ++j)
cin >> a[i][j];
for(int i = 1; i <= r; ++i)
for(int j = 1; j <= c; ++j)
mx = max(mx, dfs(i, j));
cout << mx;
return 0;
}
边栏推荐
- 简易的站点备份 Shell 脚本
- Facebook AI | learning reverse folding from millions of prediction structures
- Lifeifei: I am more like a scientist in physics than an engineer
- 带你初步了解 类和对象 的基本机制
- Nat. Commun. | 用于加速发现抗生素抗性基因的知识整合和决策支持
- 为什么宇宙会将最大速度限制在光速
- Redis operation set, Zset, hash data types and use of visualization tools
- Swift 3pThread tool Promise Pipeline Master/Slave Serial Thread confinement Serial queue
- 仅需三步学会使用低代码ThingJS与森数据DIX数据对接
- The children in the deep mountains are seeing the stars farther away
猜你喜欢

华为matepad能成为你的笔记本电脑副屏?

Web3最全搞钱秘籍,看这一篇就够了
![[the second revolution of report tools] optimize report structure and improve report operation performance based on SPL language](/img/53/d6f05e8050e27dc9d59f1196753512.png)
[the second revolution of report tools] optimize report structure and improve report operation performance based on SPL language

牛客网:两数之和

企鹅电竞停步,虎牙也难行

丢失的遗传力--Missing heritability

厉害了,工信部推出 “一键解绑” 手机号绑定的互联网账号,堪称神器

Graduation season: to you

vscode常用快捷键

Station B doesn't want to be a "conscience aiyouteng"
随机推荐
What is the highest compound interest insurance product? How much does it cost a year?
IPO治不了威马的杂症?
pands pd.DataFrame()函数详细解析
ahk常用函数
线上交流丨技能网络:解决多任务多模态问题的稀疏模型(青源Talk第19期 唐都钰)
Make good use of the industrial chain to attract investment, create industrial cluster effect and realize the coordinated development of industries
Nacos注册中心
嘿!ONES 新星请看过来|师兄师姐说
为什么 0.1+0.2=0.30000000000000004
Example analysis of SQL injection error reporting
Web3最全搞钱秘籍,看这一篇就够了
AIChE | 集成数学规划方法和深度学习模型的从头药物设计框架
What should be done to improve the service level of the park and optimize the business environment
牛客网:表达式求值
Primekg: building a knowledge map to achieve precision medicine
Online communication skill network: a sparse model for solving multi task and multi-modal problems (Qingyuan talk, issue 19, tangduyu)
Fabric.js 精简输出的JSON
如何运行plink软件--三种方法
Detailed derivation of perspective projection transformation and related applications
【玩转华为云】手把手带你使用鲲鹏代码迁移工具实现源码迁移