当前位置:网站首页>Informatics Olympiad all in one 1280: [example 9.24] skiing | openjudge noi 2.690: skiing | Luogu p1434 [show2002] skiing
Informatics Olympiad all in one 1280: [example 9.24] skiing | openjudge noi 2.690: skiing | Luogu p1434 [show2002] skiing
2022-06-10 22:44:00 【Junyi_ noip】
【 Topic link 】
ybt 1280:【 example 9.24】 skiing
OpenJudge NOI 2.6 90: skiing
Luogu P1434 [SHOI2002] skiing
【 Topic test site 】
1. Dynamic programming
2. Memory recursion
【 Their thinking 】
1. State definition
aggregate : All the routes
Limit : A route from a certain position
attribute : Route length
Conditions : The longest
statistic : Route length
State definition :dp[i][j]: from (i,j) Of all the routes to go , The length of the longest route .
2. State transition equation
Note No (i,j) The height of the position is a[i][j].
aggregate : from (i,j) All the routes of departure
Split set : Divide the set according to the position that can be reached in the next step .
The next steps are :(i-1,j), (i+1,j), (i,j-1), (i,j+1). As long as the height of this position is lower than the current (i,j) The height of the position , You can get here .
- If
a[i-1][j] < a[i][j], Then the next step is (i-1,j). from (i,j) The length of the longest route of departure , For from (i-1,j) The length of the longest route of departure plus 1, namelydp[i][j] = dp[i-1][j] + 1. - If
a[i+1][j] < a[i][j], Then the next step is (i+1,j). from (i,j) The length of the longest route of departure , For from (i+1,j) The length of the longest route of departure plus 1, namelydp[i][j] = dp[i+1][j] + 1. - If
a[i][j-1] < a[i][j], Then the next step is (i,j-1). from (i,j) The length of the longest route of departure , For from (i,j-1) The length of the longest route of departure plus 1, namelydp[i][j] = dp[i][j-1] + 1. - If
a[i][j+1] < a[i][j], Then the next step is (i,j+1). from (i,j) The length of the longest route of departure , For from (i,j+1) The length of the longest route of departure plus 1, namelydp[i][j] = dp[i][j+1] + 1. - Find the maximum value in the above four cases .
Because I'm asking (i,j) when , The state of its four positions up, down, left and right :dp[i-1][j], dp[i+1][j], dp[i][j-1], dp[i][j+1] I'm not sure it's all worked out . Therefore, the state can be solved by memory recursive method .
Let's set the function dfs(i, j), The function is to solve the state dp[i][j]. If dp[i][j] It has been worked out ( Greater than 0), Then go straight back dp[i][j]. Otherwise, the above state transition equation is recursively solved .
Traverse all positions , Find the longest length of the route starting from each position , Find the maximum of them , This is the result of the problem .
【 Solution code 】
solution 1: Dynamic programming Memory recursion
#include<bits/stdc++.h>
using namespace std;
#define N 105
int dp[N][N], a[N][N], mx, r, c;//dp[i][j]: from (i,j) The length of the longest route of departure
int dir[4][2] = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};// Direction array
int dfs(int sx, int sy)// seek dp[sx][sy]
{
if(dp[sx][sy] > 0)// If the path length is greater than 0, Has been found dp[sx][sy], Returns the value directly .
return dp[sx][sy];
int route = 0;// from (sx,sy) The maximum number of routes that can be obtained from the surrounding position
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])// If
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;
}
边栏推荐
- 很流行的状态管理库 MobX 是怎么回事?
- Digital twin: third person mouse operation
- TcaplusDB君 · 行业新闻汇编(一)
- 笔记(二)
- Visio 转为高质量PDF
- Apache相关的几个安全漏洞修复
- [applet] the vant wearp radio radio radio component cannot trigger the bind:change event
- TcaplusDB君 · 行业新闻汇编(五)
- TcaplusDB君 · 行业新闻汇编(六)
- How can small and medium-sized conferences be upgraded digitally?
猜你喜欢

TcaplusDB君 · 行业新闻汇编(三)

【小程序】Vant滑动单元格添加点击其他位置自动关闭的功能

datagrip 报错 “The specified database user/password combination is rejected...”的解决方法

(11) Tableview

【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍
![[tcapulusdb knowledge base] Introduction to tcapulusdb engine parameter adjustment](/img/74/6ce32e007c064c9255269fe38761a4.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb engine parameter adjustment
![[tcapulusdb knowledge base] tcapulusdb tcapdb capacity expansion and contraction introduction](/img/3b/6546846fb7bbccbb0abe91422549ed.png)
[tcapulusdb knowledge base] tcapulusdb tcapdb capacity expansion and contraction introduction

Web3 ecological decentralized financial platform sealem Finance
![[tcapulusdb knowledge base] tcapulusdb refresh tbus channel introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb refresh tbus channel introduction

Latex error: file ‘xxx. sty‘ not found
随机推荐
GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.
leetcode 130. Surrounded Regions 被围绕的区域(中等)
Sealem Finance打造Web3去中心化金融平台基础设施
手机号码更新不出来,怎么处理
How can small and medium-sized conferences be upgraded digitally?
CCF CSP 202109-2 非零段划分【100分】
torch_ geometric
【TcaplusDB知识库】TcaplusDB推送配置介绍
leetcode:333. Maximum BST subtree
How small and micro enterprises build micro official websites at low cost
【TcaplusDB知识库】TcaplusDB TcapDB扩缩容介绍
Notes (V) - JVM
1.Tornado简介&&本专栏搭建tornado项目简介
Solution to "XXX has broken path" error in idea
CCF CSP 202109-3 impulse neural network
TcaplusDB君 · 行业新闻汇编(四)
【TcaplusDB知识库】TcaplusDB进程启动介绍
TcaplusDB君 · 行业新闻汇编(六)
dc_ Study and summary of labs--lab1
Tcapulusdb Jun · industry news collection (V)