• NUC_HomeWork1 -- POJ1088(DP)


    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个方向能否继续走下去,能走下去,就一直递归下去
     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
    ", ans + 1);
    63     }
    64     return 0;
    65 }
    View Code
  • 相关阅读:
    vue响应式原理的实现
    手写 Promise
    模拟ATM机功能(C语言)
    打印空心菱形
    假设一对耗子每个月都可以生一对小耗子。小耗子生长3个月后,从第4个月开始也就能够生小耗子。问:假设所有的耗子都不死的话,那么20个月后一共有多少只耗子?
    判断一年中的第几天
    依次将10个数输入,要求打印出其中最大的数
    九九乘法表
    判断是否为闰年
    从键盘上接收一个字母,判断是否是大写字母,如果是则转换成小写字母输出 ,否则直接输出。
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/4075252.html
Copyright © 2020-2023  润新知