• NYOJ skiing(DFS+记忆化搜索)


                                                                   skiing

                                                                           时间限制:3000 ms  |  内存限制:65535 KB
    描述
    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更长。事实上,这是最长的一条。
    输入
    第一行表示有几组测试数据,输入的第二行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
    后面是下一组数据;
    输出
    输出最长区域的长度。
    样例输入
    1
    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
    
    样例输出
    25
    在dfs中记忆化搜索
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<map>
     8 #include<stack>
     9 #include<list>
    10 #include<set>
    11 #include<vector>
    12 #include<cstdlib>
    13 #include<string>
    14 #define eps 0.000000001
    15 typedef long long ll;
    16 typedef unsigned long long LL;
    17 using namespace std;
    18 const int INF=0x3f3f3f3f;
    19 const int N=100+10;
    20 int a[N][N];
    21 int dp[N][N];
    22 int m,n;
    23 int _x[4]={0,-1,0,1};
    24 int _y[4]={1,0,-1,0};
    25 int judge(int x,int y){
    26     if(x<0||x>=n||y<0||y>=m)return 0;
    27     return 1;
    28 }
    29 int DFS(int x,int y,int t){
    30     if(judge(x,y)==0) return 0;
    31     if(a[x][y]>t) return 0;
    32     if(dp[x][y]>=0)return dp[x][y];
    33     for(int i=0;i<4;i++){
    34         int xx=x+_x[i];
    35         int yy=y+_y[i];
    36         dp[x][y]=max(dp[x][y],DFS(xx,yy,a[x][y])+1);
    37     }
    38     return dp[x][y];
    39 }
    40 int main(){
    41     int t;
    42     scanf("%d",&t);
    43     while(t--){
    44         scanf("%d%d",&n,&m);
    45         for(int i=0;i<n;i++)
    46         for(int j=0;j<m;j++){
    47             scanf("%d",&a[i][j]);
    48         }
    49         int maxx=0;
    50         memset(dp,-1,sizeof(dp));
    51         for(int i=0;i<n;i++)
    52         for(int j=0;j<m;j++){
    53             int ans=DFS(i,j,INF);
    54             maxx=max(maxx,ans);
    55         }
    56         cout<<maxx<<endl;
    57     }
    58 }
  • 相关阅读:
    5.8
    python运维自动化
    javascript学习(一)
    python学习-1
    A-GPS学习笔记(二) 之SUPL
    A-GPS学习笔记(一)
    CF756D Bacterial Melee
    LG P2495 [SDOI2011]消耗战
    LG P7325 [WC2021] 斐波那契
    LG P7324 [WC2021] 表达式求值
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6667068.html
Copyright © 2020-2023  润新知