洛谷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月之前购买的百事任何饮料的瓶盖上都会有一个百事球星的名字.只要凑齐所有百事球星的名字,就可参加百事世界杯之旅的抽 ...
随机推荐
- Go语言入门——hello world
Go 语言源代码文件扩展名是.go. 知识点:1. go语言代码的第1行必须声明包2. 入口的go语言代码(包含main函数的代码文件)的包必须是main,否则运行go程序会显示go run: can ...
- pands模块的妙用爬取网页中的表格
拿我这篇为例https://www.cnblogs.com/pythonywy/p/11574340.html import pandas as pd df = pd.read_html('https ...
- CSS文本单行或者多行超出区域省略号(...)显示方法
单行超出时,主要用到几个CSS属性: 1.text-overflow : clip | ellipsis ; clip : 不显示省略标记(...),而是简单的裁切ellipsis : 当对象内文本溢 ...
- Java中遇到Case cannot be resolved to a variable问题
Java中遇到Case cannot be resolved to a variable问题 记录一下这两天项目中遇到的一个小问题. 在项目中遇到一个问题,一直显示 Case cannot be ...
- selenium登录简单的网站
import time from selenium import webdriver from lxml import etree from selenium.webdriver import Act ...
- 第一册:lesson 131.
原文: Don't be so sure. question:What's the problem about deciding on a holiday. Where are you going t ...
- centos7.5 安装java11
jdk的下载地址如下: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html 第一 ...
- shell脚本返回值问题
如果学习过高级语言比如java和c语言等,此时你要是获取一个函数的返回值,直接在函数里面写上return即可,然后在函数执行时将返回结果赋值给某个变量即可.但是在shell脚本中限制较多,因此如果我们 ...
- css3动画 2D 3D transfrom
2D transform 例如transform: translate(1px,30px); translate() 方法 translate()方法,根据左(X轴)和顶部(Y轴)位置给定的参数,从当 ...
- js图片转为base64的格式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...