[vijos1011] Skiing

Source: Internet
Author: User

Mob Search

1#include <cstdio>2#include <iostream>3#include <cstring>4 using namespacestd;5 6 intnum[505][505];7 intans=1, n,m;8 intfx[4]={0,0,1,-1};9 intfy[4]={1,-1,0,0};Ten  One voidDfsintXintYintLen) { Aans=Max (Ans,len); -      for(intI=0;i<4; i++) -       if(num[x][y]>num[x+fx[i]][y+fy[i]]&&num[x+fx[i]][y+fy[i]]!=-1) theDFS (x+fx[i],y+fy[i],len+1); - } -  - intMain () { +Freopen ("ski.in","R", stdin); -Freopen ("Ski.out","W", stdout); +scanf"%d%d",&n,&m); Amemset (num,-1,sizeof(num)); at      for(intI=1; i<=n;i++) -        for(intj=1; j<=m;j++) -scanf"%d",&num[i][j]); -      for(intI=1; i<=n;i++) -        for(intj=1; j<=m;j++){ -           BOOLis_ok=0; in            for(intt=0; t<=3; t++) -             if(num[i][j]>num[i+fx[t]][j+fy[t]]&&num[i+fx[t]][j+fy[t]]!=-1) is_ok=1; to           if(is_ok==1) DFS (I,J,1);  +       } -printf"%d", Ans); the     return 0; *}
View Code

Direct search does not work well with overlapping sub-problems, so memory search can be used to reduce time

Memory Search

1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 intf[505][505];//store the longest path that can be slid from (I,J) to avoid duplicate searches7 intnum[505][505];8 intfx[4]={0,0,1,-1};9 intfy[4]={1,-1,0,0};Ten  One intDfsintXinty) { A     inttmp=0; -     if(f[x][y]!=-1)returnF[x][y]; -      for(intI=0;i<4; i++){ the         if(num[x][y]>num[x+fx[i]][y+fy[i]]&&num[x+fx[i]][y+fy[i]]!=-1) -Tmp=max (Tmp,dfs (x+fx[i],y+fy[i]) +1); -     } -     if(tmp==0) { +f[x][y]=1; -         return 1; +     } Af[x][y]=tmp; at     returntmp; - } - intMain () { -     intans=1; -Freopen ("ski2.in","R", stdin); -Freopen ("Ski2.out","W", stdout); in     intn,m; -scanf"%d%d",&n,&m); tomemset (num,-1,sizeof(num)); +memset (f,-1,sizeof(f)); -      for(intI=1; i<=n;i++) the        for(intj=1; j<=m;j++) *scanf"%d",&num[i][j]); $      for(intI=1; i<=n;i++)Panax Notoginseng        for(intj=1; j<=m;j++) -ans=Max (Ans,dfs (i,j)); theprintf"%d\n", ans); +     return 0; A}
View Code

[vijos1011] Skiing

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.