POJ 1088 滑雪 记忆化搜索
2015-10-15 17:13
561 查看
滑雪
Description
Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为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
Sample Output
Source
SHTSC 2002
dp[i][j]表示长度,从i=1,j=1;开始搜索如果四周有大于a[i][j]例如a[i-1][j]那么递归求temp=max(a[i-1][j],dfs(i-1,j)
循环跟新每个点的高度最后循环求出dp[i][j]的最大值。
ACcode:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 85082 | Accepted: 31876 |
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
dp[i][j]表示长度,从i=1,j=1;开始搜索如果四周有大于a[i][j]例如a[i-1][j]那么递归求temp=max(a[i-1][j],dfs(i-1,j)
循环跟新每个点的高度最后循环求出dp[i][j]的最大值。
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 105
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
using namespace std;
int a[maxn][maxn],dp[maxn][maxn],n,m;
int dfs(int x,int y){
if(dp[x][y]!=0)
return dp[x][y];
//if(x<1||x>n||y<1||y>m)
// return 0;
if(x==0 || x==n+1 || y==0 || y==m+1)
return 0;
int cnt=0;
if(a[x-1][y]<a[x][y])
cnt=max(cnt,dfs(x-1,y));
if(a[x+1][y]<a[x][y])
cnt=max(cnt,dfs(x+1,y));
if(a[x][y-1]<a[x][y])
cnt=max(cnt,dfs(x,y-1));
if(a[x][y+1]<a[x][y])
cnt=max(cnt,dfs(x,y+1));
dp[x][y]=cnt+1;
return cnt+1;
}
int main(){
while(rd2(n,m )!=EOF){
MT(dp,0);
FOR(i,1,n)
FOR(j,1,m)
rd(a[i][j]);
FOR(i,1,n)
FOR(j,1,m)
dfs(i,j);
int maxxx=-1;
FOR(i,1,n)
FOR(j,1,m)
maxxx=max(maxxx,dp[i][j]);
printf("%d\n",maxxx);
}
return 0;
}
/*
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*/
相关文章推荐
- QProcess运行bcdedit命令
- myeclipse10安装emmet插件
- JRE System Library 与Java EE5 Libraries的区别
- 深入JVM关闭与关闭钩子
- 快速解决IMP中的ORA-00959
- C语言容易混淆的问题
- Maven实战 第6章 仓库
- CMUSphinx---pocketsphinx-5prealpha 安装
- ALOGV
- 写代码与写文章
- 从头认识java-1.4 复用代码
- Adb connection Error:远程主机强迫关闭了一个现有的连接
- linux消息队列的内核限制
- 一些有趣的图形界面
- python cmd 模块使用
- Java enum的用法详解
- maven 常用命令
- 从头认识java-1.4 复用代码
- 当看到《疯狂Java程序员的修养》这段代码的时候,我是真的搞蒙了,不知道怎么一回事代码如下:
- PPM / PGM / PBM 图像文件格式