DFS:POJ1088-滑雪(记忆化搜索)
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 97666
Accepted: 37055
Description
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。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。
Input
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
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
Sample Output
25
Source
SHTSC 2002
#include<stdio.h>
const int maxn = 110;
int n,m;
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
int start_x,start_y;
int Max = 0;
struct Maps
{
int num;
int step;
} maps[maxn][maxn]; bool check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m)
return false;
else
return true;
}
void pre_maps()
{
Max = -1;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%d",&maps[i][j].num);
maps[i][j].step = 0;
if(Max < maps[i][j].num)
{
Max = maps[i][j].num;
start_x = i;
start_y = j;
}
}
}
} void dfs(int x,int y,int step)
{
if(step > Max)//直接用Max记录一下最远的路径就可以了
Max = step;
if(step < maps[x][y].step)
return ;
else
maps[x][y].step = step;
for(int i=0; i<4; i++)
{
if(check(x+dir[i][0],y+dir[i][1]) && (maps[x+dir[i][0]][y+dir[i][1]].num < maps[x][y].num))
{
dfs(x+dir[i][0],y+dir[i][1],step+1);
}
}
} int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
pre_maps();
Max = -1;
dfs(start_x,start_y,1);//这一步主要是为了优化
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
dfs(i,j,1);
}
printf("%d\n",Max);
}
}
DFS:POJ1088-滑雪(记忆化搜索)的更多相关文章
- POJ-1088 Skiing(记忆化搜索)
Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- tyvj 1004 滑雪 记忆化搜索
滑雪 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.tyvj.cn/p/1004 Description trs喜欢滑雪.他来 ...
- poj1088(记忆化搜索入门题)
题目链接:http://poj.org/problem?id=1088 思路: 明显的记忆化搜索题,用dp[i][j]表示从(i,j)出发能滑的最远距离,用dfs搜索,若dp[x][y]>0即已 ...
- 洛谷-P1434 [SHOI2002]滑雪 (记忆化搜索)
题意:有一个\(R*C\)的矩阵,可以从矩阵中的任意一个数开始,每次都可以向上下左右选一个比当前位置小的数走,求走到\(1\)的最长路径长度. 题解:这题很明显看到就知道是dfs,但是直接爆搜会TLE ...
- POJ 1088 滑雪 (记忆化搜索)
题目链接:http://poj.org/problem?id=1088 题意很好懂,就是让你求一个最长下降路线的长度. dp[i][j]记录的是i j这个位置的最优的长度,然后转移方程是dp[i][j ...
- POJ 1088 滑雪(记忆化搜索+dp)
POJ 1088 滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 107319 Accepted: 40893 De ...
- hdu1078 dfs+dp(记忆化搜索)搜索一条递增路径,路径和最大,起点是(0,0)
#include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; ty ...
- POJ1088滑雪(记忆化搜索)
就是用DP,DP[i][j]是在这个(i,j)位置作为起点的最长长度. 因为可能会超时,DP的话每次就是记录,然后就不用回溯了. 很简单的DFS里面的记忆化搜索. #include <stdio ...
- 蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索
问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...
- hdu 1428(很好的一道题,最短路+记忆化搜索)
漫步校园 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- The sixth day
bound to 铁定You are bound to be fired 你会被铁定开除的 A:Dan forgot his map? Dan忘了带地图了吗? B:Yep!And he's boun ...
- vue $set用法
需求,想给下面的数据添加一个hoby属性 {{data.hoby}}-->让这里的视图改变 data:{ name: "简书", age: '3', info: { cont ...
- 入口类和@SpringBootApplication
SpringBoot通常有一个名为*Application的入口类,入口类里有一个标准的Java应用的入口方法,main方法,在该方法中使用SpringApplication.run(xxxxxApp ...
- 【extjs6学习笔记】1.10 初始: 定义类
http://www.extjs-tutorial.com/extjs/define-new-class-in-extjs
- EEC 欧姆龙PLC输入模块算法
Option Explicit Public MyArray(20000) As Integer Public MyArraySensor(20000) As Integer Sub 生成输入 ...
- ionic文本颜色
需添加"ion-text"使"color='light'"生效 <div text-center ion-text color="light&q ...
- 微信jssdk实现分享到微信
本来用的是这个插件http://overtrue.me/share.js/和百度分享 相同之处:在微信分享的时候,分享的链接都不能获取到缩略图... 不同之处:百度分享在微信低版本是可以看到缩略图的( ...
- IOS 九宫图解锁(封装)
NJLockView.h /.m @class NJLockView; @protocol NJLockViewDelegate <NSObject> - (void)lockViewDi ...
- hdu-1179 Ollivanders: Makers of Fine Wands since 382 BC.---二分图匹配模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1179 题目大意: 有n个人要去买魔杖,有m根魔杖(和哈利波特去买魔杖的时候一样,是由魔杖选人).接下 ...
- 关于profile集合
profile集合是mongodb的慢操作日志 > db.getProfilingStatus() { , , } 可以通过getProfilingStatus来查看当前profile设置 pr ...