uva10285 Longest Run on a Snowboard(dp之记忆化搜索 )

10285 Longest Run on a Snowboard

Michael likes snowboarding. That’s not very surprising, since snowboarding is really great. The bad

thing is that in order to gain speed, the area must slide downwards. Another disadvantage is that when

you’ve reached the bottom of the hill you have to walk up again or wait for the ski-lift.

Michael would like to know how long the longest run in an area is. That area is given by a grid of

numbers, defining the heights at those points. Look at this example:

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

One can slide down from one point to a connected other one if and only if the height decreases. One

point is connected to another if it’s at left, at right, above or below it. In the sample map, a possible

slide would be 24-17-16-1 (start at 24, end at 1). Of course if you would go 25-24-23-…-3-2-1, it would

be a much longer run. In fact, it’s the longest possible.

Input

The first line contains the number of test cases N . Each test case starts with a line containing the

name (it’s a single string), the number of rows R and the number of columns C. After that follow R

lines with C numbers each, defining the heights. R and C won’t be bigger than 100, N not bigger than

15 and the heights are always in the range from 0 to 100.

Output

For each test case, print a line containing the name of the area, a colon, a space and the length of the

longest run one can slide down in that area.

Sample Input

2

Feldberg 10 5

56 14 51 58 88

26 94 24 39 41

24 16 8 51 51

76 72 77 43 10

38 50 59 84 81

5 23 37 71 77

96 10 93 53 82

94 15 96 69 9

74 0 62 38 96

37 54 55 82 38

Spiral 5 5

1 2 3 4 5

16 17 18 19 6

15 24 25 20 7

Universidad de Valladolid OJ: 10285 – Longest Run on a Snowboard 2/2

14 23 22 21 8

13 12 11 10 9

Sample Output

Feldberg: 7

Spiral: 25

记忆化搜索

/*
Solve:
*/
#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<cstring>
#include<stack>
#include<string>
#include<set>
#include<fstream>
using namespace std;
#define pb push_back
#define cl(a,b) memset(a,b,sizeof(a))
#define bug printf("===\n");
#define rep(a,b) for(int i=a;i<b;i++)
#define rep_(a,b) for(int i=a;i<=b;i++)
#define P pair<int,int>
#define X first
#define Y second
#define vi vector<int>
const int maxn=105;
const int inf=999999999;
typedef long long LL;
void Max(int&a,int b){if(a<b)a=b;}
char name[50];
int a[maxn][maxn];
int dp[maxn][maxn];//end with (i,j) you can get the max length
int n,m;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

int dfs(int x,int y){//表示从(x,y)这个点出发,所能到达的最大长度

    if(dp[x][y]){
        return dp[x][y];
    }
    int ans=0;
    for(int i=0;i<4;i++){
        int xx=dx[i]+x;
        int yy=dy[i]+y;
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[xx][yy]<a[x][y]){
            ans=max(ans,dfs(xx,yy));//记录下这个点出发的四个方向中最大的那个

        }
    }
    dp[x][y]+=ans+1;//最大值加上当前这个点

    return dp[x][y];
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s%d%d",name,&n,&m);
        int mxv=0,pi,pj;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%d",&a[i][j]);
                if(a[i][j]>mxv){
                    mxv=a[i][j];
                    pi=i;
                    pj=j;
                }
            }
        }
        cl(dp,0);
        int mx=1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                mx=max(mx,dfs(i,j));//遍历每一个点
            }
        }
        printf("%s: %d\n",name,mx);
    }
    return 0;
}
/*
2
Feldberg 10 5
56 14 51 58 88
26 94 24 39 41
24 16 8 51 51
76 72 77 43 10
38 50 59 84 81
5 23 37 71 77
96 10 93 53 82
94 15 96 69 9
74 0 62 38 96
37 54 55 82 38
Spiral 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
*/

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-30 10:06:20

uva10285 Longest Run on a Snowboard(dp之记忆化搜索 )的相关文章

uva10285 - Longest Run on a Snowboard(记忆化搜索)

题目:uva10285 - Longest Run on a Snowboard(记忆化搜索) 题目大意:给出N * N的矩阵,要求找到一条路径,路径上的值是递减的,求这样的路径的最长长度. 解题思路:记忆话搜索.因为要求最长的路径那么就需要将所有的这样的路径找出,但是直接dfs会超时的.对于同一个位置,从这个点出发的最长路径长度是固定的.所以在找的时候就要将这个位置的最长路径算出来并且保存下来.一开始还担心会走回头路就是一直兜圈子.但是后面发现能往右走就不可能在从右边的那个位置走回去,因为路径

poj1179 区间dp(记忆化搜索写法)有巨坑!

http://poj.org/problem?id=1179 Description Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an integer and each edge is labelled with either the symbol + (add

Codeforces 509F Progress Monitoring (区间dp 或 记忆化搜索)

F. Progress Monitoring time limit per test 1 second memory limit per test 256 megabytes Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students: You are given a tree T with n vertices, specified b

HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5948    Accepted Submission(s): 2191 Problem Description Jimmy experiences a lot of stress at work these days, especial

POJ 1958 Strange Towers of Hanoi (线性dp,记忆化搜索)

JQuery工具方法. (1)$.isNumeric(obj) 此方法判断传入的对象是否是一个数字或者可以转换为数字. isNumeric: function( obj ) { // parseFloat NaNs numeric-cast false positives (null|true|false|"") // ...but misinterprets leading-number strings, particularly hex literals ("0x...&

蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索

问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走过某个格子时,如果那个格子中的宝贝价值比小明手中任意宝贝价值都大,小明就可以拿起它(当然,也可以不拿). 当小明走到出口时,如果他手中的宝贝恰好是k件,则这些宝贝就可以送给小明. 请你帮小明算一算,在给定的局面下,他有多少种不同的行动方案能获得这k件宝贝. 输入格式 输入一行3个整数,用空格分开:n

POJ 1958 Strange Towers of Hanoi (四塔问题,线性dp,记忆化搜索)

题目分析:四柱汉诺塔.由于题目已经给出了求解方法,直接写代码即可.下面总结一下,四塔问题. 感谢这篇文章的作者,点这里就到,总结的很好.直接贴过来~ 四塔问题:设有A,B,C,D四个柱子(有时称塔),在A柱上有由小到大堆放的n个盘子. 今将A柱上的盘子移动到D柱上去.可以利用B,C柱作为工作栈用,移动的规则如下: ①每次只能移动一个盘子. ②在移动的过程中,小盘子只能放到大盘子的上面. 设计并实现一个求解四塔问题的动态规划算法,并分析时间和空间复杂性. 算法思想: 用如下算法移动盘子(记为Fou

UVa 10285 Longest Run on a Snowboard(DP 二维最长递减子序列)

题意  输入一个城市的滑雪地图  你可以从高的地方滑到伤下左右低的地方  求这个城市的最长滑雪线路长度   即在一个矩阵中找出最长递减连续序列 令d[i][j]为以格子map(i,j)为起点的最长序列   则有状态转移方程d[i][j]=max{d[a][b]}+1  a,b为与i,j相邻且值比i,j小的所有点 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #def

UVA10285 Longest Run on a Snowboard

题解: 记忆化搜索入门题 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define LL long long #define CLR(x) memset(x,0,sizeof x) #define MC(x,y) memcpy(x,y,sizeof(x)) #define SZ(x) (