UVA-10285 longest Run on a Snowboard (recursive)

Source: Internet
Author: User

Topic: Skiing. Give a two-dimensional array to find the length of the longest continuous descent sequence.

Title Analysis: Defines the length of the longest continuous descent sequence that DP (I,J) represents at the end of A[i][j], then DP (I,J) =max (DP (I-1,J), DP (I+1,J), DP (I,J-1), DP (I,J+1)), Repeat this DP process to update the DP array 100 times (I have done a test specifically, the number of updates in 42 times and above can be AC).

The code is as follows:

# include<iostream># include<cstdio># include<string># include<cstring># include< Algorithm>using namespace Std;int a[105][105],dp[105][105];int n,m;string p;int d[4][2]={{-1,0},{1,0},{0,1},{0,-1 }};bool judge (int x,int y) {return x>=0&&x<n&&y>=0&&y<m;}    int solve () {for (Int. i=0;i<n;++i) for (int j=0;j<m;++j) dp[i][j]=1; for (int k=0;k<42;++k) {for (int x=0;x<n;++x) {for (int. y=0;y<m;++y) {for (int) {I=0;i                    <4;++i) {int nx=x+d[i][0],ny=y+d[i][1]; if (judge (Nx,ny) &&a[x][y]>a[nx][ny]&&dp[x][y]+1>dp[nx][ny]) dp[nx][ny]=dp[x][y                ]+1;    }}}} int ans=0;    for (int. i=0;i<n;++i) for (int j=0;j<m;++j) Ans=max (Ans,dp[i][j]); return ans;}    int main () {int T;    scanf ("%d", &t);    while (t--) {cin>>p;    scanf ("%d%d", &n,&m);        for (int i=0;i<n;++i) for (int j=0;j<m;++j) scanf ("%d", &a[i][j]);    cout<<p<< ":" <<solve () <<endl; } return 0;}

  

UVA-10285 longest Run on a Snowboard (recursive)

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.