POJ 【1088】 滑雪
滑雪
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 35399 Accepted: 12399
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
题意:找到一个点的高度,从这个点的四方向,寻找比它高度低的点,记录能够找到点的数量
思路:遍历每个点,记录这个能找比它低的数量,后面如果再通过这个点的时候就可以直接去用
记忆化搜索,有点像递归(作为一只小菜鸡原来是没有什么思路的,看了网上大神的代码思路,这才懂了一丢丢)
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<iterator>
#include<vector>
#include<stdlib.h>
#include<map>
typedef long long ll;
using namespace std;
int v[110][110];
int a[110][110];
int ans;
int n,m,k,mx;
int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int dfs(int x,int y)
{
if(v[x][y]>0)
return v[x][y];//如果之前已经有过记录,便返回这个记录的值
int fx,fy,mx1=1;
for(int i=0;i<4;i++){
fx = x+dir[i][0];
fy = y+dir[i][1];
if(fx>=0&&fx<n&&fy>=0&&fy<m&&a[fx][fy]<a[x][y]){
mx1 = max(dfs(fx,fy)+1,mx1);//继续向其他方向进行前进
}
}
v[x][y] = mx1;
return mx1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF){
mx = 0;
memset(v,0,sizeof(v));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
mx = max(dfs(i,j),mx);//遍历每个点,记录最大的值
}
}
printf("%d\n",mx);
}
return 0;
}