开始用的BFS。。。超时。。。。
AC代码如下:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
int x, y;
int h;
bool operator<( const Node &b )const{
return h < b.h;
}
};
int M, N;
int num[110][110];
int moves[][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
int dp[110][110];
int DFS( int x, int y ){
if( dp[x][y] != 0 ){
return dp[x][y];
}
int ans = 0;
for( int i = 0; i < 4; i++ ){
int tempx = x + moves[i][0];
int tempy = y + moves[i][1];
if( tempx < 0 || tempx >= M || tempy < 0 || tempy >= N || num[tempx][tempy] <= num[x][y] ){
continue;
}
ans = max( ans, DFS( tempx, tempy ) + 1 );
}
return dp[x][y] = ans;
}
int solve(){
bool mark[110][110];
int max_ans = -1;
memset( mark, false, sizeof( mark ) );
memset( dp, 0, sizeof( dp ) );
for( int i = 0; i < M; i++ ){
for( int j = 0; j < N; j++ ){
max_ans = max( max_ans, DFS( i, j ) );
}
}
return max_ans + 1;
}
int main(){
while( scanf( "%d%d", &M, &N ) != EOF ){
for( int i = 0; i < M; i++ ){
for( int j = 0; j < N; j++ ){
cin >> num[i][j];
}
}
int ans = solve();
cout << ans << endl;
}
return 0;
}