洛谷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月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽 ...
随机推荐
- 思维导图xmind的文档保存问题
如果文件名相同,可能最新的文档覆盖以前的.当前活动文档只能有一个,如果有多个,保存后,其他活动文档也被更新了. 新建一个空白doc文档,仅仅是文件名,作为附件导入到xmind中,在xmind中保存后, ...
- C# vb .NET读取识别条形码线性条码code39
code39是比较常见的条形码编码规则类型的一种.如何在C#,vb等.NET平台语言里实现快速准确读取该类型条形码呢?答案是使用SharpBarcode! SharpBarcode是C#快速高效.准确 ...
- Django---Django中使用COOKIE和SESSION
Django---Django中使用COOKIE和SESSION 一丶Cookie cookie的由来 # HTTP协议是无状态的. # 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请 ...
- Windows 10 下 GCC / LLVM 的安装和使用
win10下gcc的安装和makehttps://www.jianshu.com/p/46824c62dfed 方案1:直接官方下载安装minGw或Cygwininstall download: ht ...
- 安装部署Spark 1.x Standalone模式集群
Configuration spark-env.sh HADOOP_CONF_DIR=/opt/data02/hadoop-2.6.0-cdh5.4.0/etc/hadoop ...
- 记一则 Lambda内递归调用方法将集合对象转换成树形结构
public dynamic GetDepartments(string labID) { List<int> usedIDs = new List<int>(); //缓存已 ...
- 爬虫之selenium模块;无头浏览器的使用
一,案例 爬取站长素材中的图片:http://sc.chinaz.com/tupian/gudianmeinvtupian.html import requests from lxml import ...
- ansible之REPLACE模块
> REPLACE (/usr/lib/python2.7/site-packages/ansible/modules/files/replace.py) This module will re ...
- urdf 学习记录
1.URDF(Unified Robot Description Format),统一的机器人描述文件格式.主要用来描述机器人的几何形状,在可视化时(如RViz中)显示出机器人的几何形状.与画图软件( ...
- Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested ...