Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我的做法,最简单的深搜,效果很好,760k,16ms..大家可以借鉴一下

Posted by smallwood at 2009-09-02 16:10:17 on Problem 1088
#include<iostream>
using namespace std;
//a用来存输入矩阵
int a[105][105];
//b是寸从某一点出发得到的最长路径
int b[105][105];
int m,n;
//这个函数是用来搜索从a[x][y]这一点开始的最长路径,使用了深入优先搜索
int f(int x,int y){
	int max=1;
	int temp=0;
	if(b[x][y]==0){
		if(x>0&&a[x-1][y]<a[x][y]){
		     temp=f(x-1,y)+1;
			 if(temp>max) max=temp;
		}
		if(x<m-1&&a[x+1][y]<a[x][y]){
		     temp=f(x+1,y)+1;
			 if(temp>max) max=temp;
		}
		if(y>0&&a[x][y-1]<a[x][y]){
			temp=f(x,y-1)+1;
			if(temp>max) max=temp;
		}
		if(y<n-1&&a[x][y+1]<a[x][y]){
			temp=f(x,y+1)+1;
			if(temp>max) max=temp;
		}
        b[x][y]=max;
	}
	
    return b[x][y];

    
}
int main(){
    
	while(cin>>m>>n){
		
	    for(int i=0;i<m;i++)
			for(int j=0;j<n;j++){
				b[i][j]=0;
				cin>>a[i][j];
			}

		int mmm=0;
		//枚举从任意一点出发的最长路径,取最大值
		for(int s=0;s<m;s++)
			for(int t=0;t<n;t++){
			    int ttt=f(s,t);
				if(ttt>mmm) mmm=ttt;
			}
		cout<<mmm<<endl;
	}

	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator