[Ultraviolet A] 10285-Longest run on a snowboard (Dynamic Planning)

Source: Internet
Author: User

This question is simple and you do not need to print the path.

State Equation DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1], DP [I + 1] [J], DP [I] [J + 1]);

14003395 10285 Longest run on a snowboard Accepted C ++ 0.026 11:43:51

Enumerate each vertex for traversal and use the memory to search. Will not time out.

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<stack>#include<queue>#include<map>#include<set>#include<list>#include<string>#include<sstream>#include<ctime>using namespace std;#define _PI acos(-1.0)#define INF (1 << 10)#define esp 1e-6typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> pill;/*======================================================================================*/#define MAXD 100 + 10char name[MAXD];int m,n,ans;int dp[MAXD][MAXD];int mat[MAXD][MAXD];int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};int DP(int x,int y){    if(dp[x][y] != -1)        return dp[x][y];    dp[x][y] = 1;    for(int i = 0 ; i < 4 ; i++){        int _x = dir[i][0] + x;        int _y = dir[i][1] + y;        if(_x >= 0 && _y >=0 && _x < n && _y < m && mat[x][y] < mat[_x][_y]){            dp[x][y] = max(dp[x][y],DP(_x,_y) + 1);        }    }    ans = max(ans,dp[x][y]);    return dp[x][y];}int main(){    int T;    scanf("%d",&T);    while(T--){        scanf("%s%d%d",name,&n,&m);        int pos_x,pos_y;        int MIN = INF;        memset(dp,-1,sizeof(dp));        for(int i = 0 ; i < n ; i++)            for(int j = 0 ; j < m ; j++){                scanf("%d",&mat[i][j]);                if(mat[i][j] < MIN){                    MIN = mat[i][j];                    pos_x = i;                    pos_y = j;                }            }        ans = 0;        for(int i = 0 ; i < n ; i++)            for(int j = 0 ; j < m ; j++)                DP(i,j);        printf("%s: %d\n",name,ans);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.