Vj1011: memory-based search
This is a simple memory-based search, which is exactly the same as the typical skiing question.
For the memory-based search, I also learned some things during the summer vacation after reading the question of CCY.
Actually, this is DFS + mark.
Main Parts
int search(int x,int y){if(f[x][y]>0) return f[x][y];int ans=0;int xx,yy;for(int i=0;i<4;i++){ xx=x+dx[i]; yy=y+dy[i]; if(xx>0 && xx<=n && yy>0 && yy<=m && a[x][y]>a[xx][yy]){ ans=max(ans,search(xx,yy)); }}return f[x][y]=ans+1; }
Well, I can't tell you how to use memorizing to search for such questions.
The code is quite simple:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <algorithm>#include <cmath>using namespace std;const int dx[5]={0,0,1,-1},dy[5]={1,-1,0,0};int a[501][501],f[501][501];int h,n,m,maxn=0,ans;int max(int a,int b){return a>b?a:b;}int search(int x,int y){if(f[x][y]>0) return f[x][y];int ans=0;int xx,yy;for(int i=0;i<4;i++){ xx=x+dx[i]; yy=y+dy[i]; if(xx>0 && xx<=n && yy>0 && yy<=m && a[x][y]>a[xx][yy]){ ans=max(ans,search(xx,yy)); }}return f[x][y]=ans+1; }int main(){ memset(f,-1,sizeof(f));scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];ans=-1;for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ if(f[i][j]==-1) ans=max(ans,search(i,j)); }printf("%d\n",ans);return 0;}
Vj1011: memory-based search