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

NUC_HomeWork1 -- POJ1088(DP)

时间:2014-11-05 01:50:18      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   for   

D - 滑雪

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

小白上的记忆化搜索
int d(int i, int j)
{
  if(d[i][j] >= 0)
    return d[i][j];
  return d[i][j] = a[i][j] + (i == n ? 0 : d(i+1, j) >? d[i+1][j+1]);
}
同本题很想啊
弄一个dis[][]数组保存距离,然后看它4个方向能否继续走下去,能走下去,就一直递归下去
bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 const int MAXN = 110;
 7 int arr[MAXN][MAXN];
 8 int dis[MAXN][MAXN];
 9 int mov[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
10 int line, col;
11 
12 bool test(int x, int y)
13 {
14     if(x >= 0 && x < line && y >= 0 && y < col)
15         return true;
16     return false;
17 }
18 
19 int d(int x, int y)
20 {
21     int tmp;
22     if(dis[x][y])
23         return dis[x][y];
24 
25     for(int i = 0; i < 4; ++i)
26     {
27         int xx = x + mov[i][0];
28         int yy = y + mov[i][1];
29         if(test(xx, yy))
30         {
31             if(arr[x][y] > arr[xx][yy])
32             {
33                 tmp = d(xx, yy);
34                 dis[x][y] = dis[x][y] > tmp ? dis[x][y] : tmp + 1;
35             }
36         }
37     }
38     return dis[x][y];
39 }
40 
41 int main()
42 {
43 
44     while(scanf("%d %d", &line, &col) != EOF)
45     {
46         for(int i = 0; i < line; i++)
47             for(int j = 0; j < col; j++)
48                 scanf("%d", &arr[i][j]);
49 
50          memset(dis, 0, sizeof(dis));
51 
52          int ans = 0, tmp;
53          for(int i = 0; i < line; i++)
54          {
55              for(int j = 0; j < col; j++)
56              {
57                  tmp = d(i, j);
58                  ans = ans > tmp ? ans : tmp;
59              }
60          }
61 
62          printf("%d\n", ans + 1);
63     }
64     return 0;
65 }
View Code

 

NUC_HomeWork1 -- POJ1088(DP)

标签:des   style   blog   http   io   color   ar   os   for   

原文地址:http://www.cnblogs.com/ya-cpp/p/4075252.html

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