洛谷P1434 [SHOI2002]滑雪
题目描述
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(从24开始,在1结束)。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
输入格式
输入的第一行为表示区域的二维数组的行数R和列数C(1≤R,C≤100)。下面是R行,每行有C个数,代表高度(两个数字之间用1个空格间隔)。
输出格式
输出区域中最长滑坡的长度。
输入输出样例
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
解析:
记忆化搜索
输入的是g数组
在记录答案时使用的是f数组
一开始f数组都初始化为1
然后两重循环从每一个点都开始搜一遍
注意限定条件是只能从大的滑向小的,是严格小于,寻求最大值。
爆搜可以得到90pts的好成绩
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define re register
#define Max 110
#define D double
#define gc getchar
inline int read(){
int a=;int f=;char p=gc();
while(!isdigit(p)){f|=p=='-';p=gc();}
while(isdigit(p)){a=a*+p-'';p=gc();}
return f?-a:a;
}
int n,m,g[Max][Max],ans=;
bool vis[Max][Max]={};
void dfs(int x,int y,int step)
{
ans=std::max(ans,step);
if(x->= && g[x-][y]<g[x][y] && vis[x-][y]==)
vis[x-][y]=,dfs(x-,y,step+),vis[x-][y]=;
if(x+<=n && g[x+][y]<g[x][y] && vis[x+][y]==)
vis[x+][y]=,dfs(x+,y,step+),vis[x+][y]=;
if(y->= && g[x][y-]<g[x][y] && vis[x][y-]==)
vis[x][y-]=,dfs(x,y-,step+),vis[x][y-]=;
if(y+<=m && g[x][y+]<g[x][y] && vis[x][y+]==)
vis[x][y+]=,dfs(x,y+,step+),vis[x][y+]=;
}
int main()
{
n=read();m=read();
for(re int i = ; i <= n ; ++ i)
for(re int j = ; j <= m ; ++ j)
g[i][j]=read();
for(re int i = ; i <= n ; ++ i)
for(re int j = ; j <= m ; ++ j)
dfs(i,j,);
printf("%d",ans);
return ;
}
90分爆搜
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<iomanip>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define re register
#define Max 110
#define D double
#define gc getchar
inline int read()
{
int a=;int f=;char p=gc();
while(!isdigit(p)){f|=p=='-';p=gc();}
while(isdigit(p)){a=a*+p-'';p=gc();}
return f?-a:a;
}
int n,m,g[Max][Max],ans=,f[Max][Max];
int dfs(int x,int y)
{
if(f[x][y]!=) return f[x][y];int t=;
if(x->= && g[x-][y]<g[x][y])
t=std::max(dfs(x-,y)+,t);
if(x+<=n && g[x+][y]<g[x][y])
t=std::max(dfs(x+,y)+,t);
if(y->= && g[x][y-]<g[x][y])
t=std::max(dfs(x,y-)+,t);
if(y+<=m && g[x][y+]<g[x][y])
t=std::max(dfs(x,y+)+,t);
f[x][y]=std::max(f[x][y],t);
return f[x][y];
}
int main()
{
n=read();m=read();
for(re int i = ; i <= n ; ++ i)
for(re int j = ; j <= m ; ++ j)
g[i][j]=read(),f[i][j]=;
for(re int i = ; i <= n ; ++ i)
for(re int j = ; j <= m ; ++ j)
ans=std::max(ans,dfs(i,j));
printf("%d",ans);
return ;
}
AC 代码
洛谷P1434 [SHOI2002]滑雪的更多相关文章
- 洛谷 P1434 [SHOI2002]滑雪
这道题适合记忆化练手 毕竟总有些大佬虐题. 这个题有几个剪枝 1.记忆化 这个不用多说了吧 剪枝就是 如果 当前点到下面一个点的目前下降的高度+1 小于 下面那个点 能下降的高度 那么反过来,这个点不 ...
- 洛谷 P1434 [SHOI2002]滑雪(DP,记忆化搜索)
题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一个区域中最长 ...
- 洛谷 P1434 [SHOI2002]滑雪 解题报告
这题方法有很多, 这里介绍2种: 方法1 很容易想到搜索, bfs或dfs应该都可以, 就不放代码了: 方法2 这题还可以用 dp 来做. 做法:先将每个点按照高度从小到大排序,因为大的点只能向小的点 ...
- 洛谷-P1434 [SHOI2002]滑雪 (记忆化搜索)
题意:有一个\(R*C\)的矩阵,可以从矩阵中的任意一个数开始,每次都可以向上下左右选一个比当前位置小的数走,求走到\(1\)的最长路径长度. 题解:这题很明显看到就知道是dfs,但是直接爆搜会TLE ...
- [洛谷P1434] [SHOI2007]滑雪
题目链接: here we go 题外话: 谁能想到这是一道咕了两年的\(AC\)呢--当年是在搜索还半懂不懂的时候遇到的这道题,感觉真是难得要命()所以一直拖着不做,后面就下意识地逃避了搜索相关的内 ...
- 【洛谷1434 [SHOI2002]滑雪】记忆化搜索
AC代码 #include <bits/stdc++.h> using namespace std; #define ms(a,b) memset(a,b,sizeof(a)) typed ...
- [BFS]P1434 [SHOI2002]滑雪
P1434 [SHOI2002]滑雪 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者 ...
- 洛谷P1434滑雪讲解
题源:[戳这里] 洛谷博客链接:[戳这里] 我觉得这道题主要方法应该有两种: 动态规划 搜索 下面会分别对这两种方法进行简述 一,动态规划法首先的想法是用L(i,j)表示从点(i,j)出发能到达的最长 ...
- 洛谷 P1291 [SHOI2002]百事世界杯之旅 解题报告
P1291 [SHOI2002]百事世界杯之旅 题目描述 "--在2002年6月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽 ...
随机推荐
- SecureCRT上传本地文件到linux
1.使用crt登录到需要操作的linux系统 2.按Alt+P打开sftp传输界面 3.输入pur指令加文件路径,例如:put E://srs-3.0.zip按enter就可以 4.再返回crt界面, ...
- c#使用SoundPlayer播放wav格式音频
1.引用System.Media名称空间下的类SoundPlayer SoundPlayer player = new SoundPlayer(); 2.方法调用Play(); public vo ...
- Linux下搭建keepalive+nginx
一. 安装nginx(略) 二. 安装keepalive 下载http://www.keepalived.org/download.html 安装依赖包 yum install –y popt* gc ...
- Java之路---Day15(Collection类)
2019-11-01-22:09:09 目录 1.Collection集合的概念 2.Collection集合常用方法 3.Iterator迭代器 4.增强for 5.Collection常用工具类 ...
- 2019-07-31 Jquery
Jquery是什么? jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设计的宗旨是“ ...
- vue-cli项目中使用vw——相比flexible更原生的移动端解决方案
安装命令行输入: yarn add postcss-px-to-viewport 或 npm i postcss-px-to-viewport -save -dev 配置package.json中,在 ...
- Vim 中进行文件目录操作
Vim 中进行文件目录操作 当前文件名 我们知道Vim有48个寄存器,其中%只读寄存器中保存着当前文件路径. 例如在/home/harttle/下打开src/main.cpp,我们打印%的值: :ec ...
- Django之REST_FRAMEWORK 认证组件
Django之DRF之认证组件 # from rest_framework.views import APIView # APIView 中的 dispatch 中 执行的 self.initial( ...
- JMETER 使用BeanShell 配合 if 控制器实现逻辑控制
业务场景 在登录后,我们根据登录的响应,判断是否执行下一步的操作. 实现步骤 1.在登录采样器树中增加BeanShell 监听器. 作用是在线程上下文变量中增加一个变量,表示登录是否成功. beans ...
- Odoo onChange使用
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826155.html 转载请注明原文地址: [onchange=前端js函数,可以实现前端实时更新以及修改 ...