码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1088 滑雪 记忆化搜索

时间:2015-08-21 15:20:47      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

虽然是很老很简单的题目了,但是再一次做起来体会不同了.....

滑雪
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 79919   Accepted: 29747

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
题解:当初一直不能理解为什么能够又是dfs又是dp,现在终于理解了,因为这是一个DAG。
代码:
技术分享
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <sstream>
10 #include <queue>
11 #include <typeinfo>
12 #include <fstream>
13 #include <map>
14 #include <stack>
15 typedef long long ll;
16 using namespace std;
17 #define test freopen("1.txt","r",stdin)
18 #define maxn 2000001
19 #define mod 10007
20 #define eps 1e-9
21 const int inf=0x3f3f3f3f;
22 const ll infll = 0x3f3f3f3f3f3f3f3fLL;
23 inline int read()
24 {
25     ll x=0,f=1;char ch=getchar();
26     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
27     while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
28     return x*f;
29 }
30 inline void out(int x) {
31    if(x>9) out(x/10);
32    putchar(x%10+0);
33 }
34 //**************************************************************************************
35 int n,m;
36 int a[105][105];
37 int dp[105][105];
38 int dy[4]={-1,1,0,0};
39 int dx[4]={0,0,-1,1};
40 int cal[105][105];
41 void init()
42 {
43     n=read();m=read();
44     for(int i=1;i<=n;i++)
45         for(int j=1;j<=m;j++)
46             {a[i][j]=read();dp[i][j]=1;}
47 }
48 bool check(int y,int x)
49 {
50     if(y<1||y>n||x<1||x>m)
51         return 0;
52     return 1;
53 }
54 int dfs(int y,int x)
55 {
56     if(cal[y][x]) return dp[y][x];
57     int maxl=dp[y][x];
58     for(int i=0;i<4;i++)
59     {
60         int newx=x+dx[i];int newy=y+dy[i];
61         if(check(newy,newx)&&a[newy][newx]<a[y][x])
62         {
63             maxl=max(maxl,dfs(newy,newx)+1);
64         }
65     }
66     cal[y][x]=1;
67     return dp[y][x]=maxl;
68 }
69 int main()
70 {
71     init();
72     int ans=-1;
73     for(int y=1;y<=n;y++)
74         for(int x=1;x<=m;x++)
75     {
76         ans=max(ans,dfs(y,x));
77     }
78     printf("%d\n",ans);
79     return 0;
80 }
View Code

 

poj 1088 滑雪 记忆化搜索

标签:

原文地址:http://www.cnblogs.com/diang/p/4747611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!