Vj1011: memory-based search

Source: Internet
Author: User

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

Related Article

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.