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