| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
自我感觉一个简单易懂的解决方案【递归】#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator