POJ 1088
福利题,此前做过类似的DP,思路很简单,定义状态\(dv(i, j)\)为以\((i, j)\)为起点的最长长度
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int maxr= 105;
int r, c;
int a[maxr][maxr];
int dv[maxr][maxr];
int step[4][2]= {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool vis[maxr][maxr];
int DP(const int i, const int j)
{
if (vis[i][j]){
return dv[i][j];
}
int &v= dv[i][j];
for (int k= 0; k< 4; ++k){
int ni= i+step[k][0], nj= j+step[k][1];
if (ni>= 0 && ni< r && nj>= 0 && nj< c && a[i][j] > a[ni][nj]){
v= max(v, DP(ni, nj)+1);
}
}
vis[i][j]= 1;
return v;
}
int main()
{
scanf("%d %d", &r, &c);
for (int i= 0; i< r; ++i){
for (int j= 0; j< c; ++j){
scanf("%d", a[i]+j);
dv[i][j]= 1;
}
}
memset(vis, 0, sizeof(vis));
int ans= 0;
for (int i= 0; i< r; ++i){
for (int j= 0; j< c; ++j){
ans= max(ans, DP(i, j));
}
}
printf("%d", ans);
return 0;
}