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

自我感觉一个简单易懂的解决方案【递归】

Posted by candycloud at 2013-11-02 12:52:25 on Problem 1088
#include<iostream>

using namespace std;


int longest_route(int i,int j, int m, int n, int *a, int *b){
	int max_this = 1;
	if(b[i*n + j] == 0){
		if((i - 1 >= 0)&& (a[i*n + j] > a[(i - 1)*n + j])){
			max_this = longest_route(i - 1,j , m, n, a, b) + 1;
		}
		if(((j -1) >= 0)&& (a[i*n + j] > a[i*n + j - 1])){
			int temp = longest_route(i,j - 1 , m, n, a, b) + 1;
			max_this = max_this >= temp?max_this:temp;
		}
		if(((i +1) < m)&& (a[i*n + j] > a[(i + 1)*n + j])){
			int temp = longest_route(i + 1,j , m, n, a, b) + 1;
			max_this = max_this >= temp?max_this:temp;
		}
		if(((j + 1) >= 0)&& (a[i*n + j] > a[i*n + j + 1])){
			int temp = longest_route(i,j + 1 , m, n, a, b) + 1;
			max_this = max_this >= temp?max_this:temp;
		}
	}else{
		max_this = b[i*n + j];
	}
	b[i*n + j] = max_this;
	return max_this;
}



int main()
{
	int m,n;
	cin>>m>>n;
	int *a = new int[m*n];
	int *b = new int[m*n];
	for(int i = 0; i < m*n; ++i){
			b[i] = 0;
	}
	for(int i = 0; i < m*n; ++i){
			cin>>a[i];
	}
	int max = 0;
	for(int i = 0; i < m; ++i){
		for(int j = 0; j < n; ++j){
			int temp;
			if(b[i*n + j] == 0){
				temp = longest_route(i,j, m, n, a, b);
			}else{
				temp = b[i*n + j];
			}
			max = max >= temp?max:temp;
		}
	}
	cout<<max;
	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