我还是缺少DP的思维啊。
大家这题都是用记忆化搜索,我这个算是什么方法呢 = =。。
因为只能从高往低滑,所以就排下序,从高度最高的点开始更新周围比它低的点即可。
记忆化搜索啊,感觉就是搜过的直接return就好了。。。
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 110;
int a[MAX][MAX];
int dp[MAX][MAX];
struct NODE{ int x,y;};
bool operator<(NODE aa,NODE bb)
{
return a[aa.x][aa.y] < a[bb.x][bb.y];
}
int dir[8] = {0,1,0,-1,1,0,-1,0};
bool check(int x,int y,int n,int m)
{
return x >= 0 && x < n && y >= 0 && y < m;
}
int main()
{
int n,m;
while( ~scanf("%d%d",&n,&m) )
{
priority_queue<NODE> q;
for(int i=0; i<n; i++)
for(int k=0; k<m; k++)
{
scanf("%d",&a[i][k]);
NODE t; t.x = i; t.y = k;
q.push(t);
}
memset(dp,0,sizeof(dp));
while( !q.empty() )
{
NODE t = q.top(); q.pop();
int x = t.x, y = t.y;
for(int i=0; i<8; i+=2)
{
int aa = x + dir[i];
int bb = y + dir[i+1];
if( check(aa,bb,n,m) )
if( a[aa][bb] > a[x][y] )
dp[x][y] = max(dp[x][y],dp[aa][bb]+1);
}
}
int mmax = 0;
for(int i=0; i<n; i++)
for(int k=0; k<m; k++)
if( dp[i][k] > mmax )
mmax = dp[i][k];
printf("%d\n",mmax+1);
}
return 0;
}