算最长路,最长上升子序列的变形。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int dp[101][101], graph[101][101];
int n, r, c;
int DP(int i, int j)
{
if(dp[i][j] > 0)
return dp[i][j];
dp[i][j] = 1;
if(i-1>=0 && graph[i][j]>graph[i-1][j])
dp[i][j] = max(dp[i][j], DP(i-1, j)+1);
if(i+1<r && graph[i][j]>graph[i+1][j])
dp[i][j] = max(dp[i][j], DP(i+1, j)+1);
if(j-1>=0 && graph[i][j]>graph[i][j-1])
dp[i][j] = max(dp[i][j], DP(i, j-1)+1);
if(j+1<c && graph[i][j]>graph[i][j+1])
dp[i][j] = max(dp[i][j], DP(i, j+1)+1);
return dp[i][j];
}
int main()
{
#ifdef test
freopen("input.txt", "r", stdin);
#endif
char name[100];
scanf("%d", &n);
while(n--)
{
scanf("%s%d%d", name, &r, &c);
for(int i=0; i<r; ++i)
for(int j=0; j<c; ++j)
{
scanf("%d", &graph[i][j]);
dp[i][j] = 0;
}
int _max = 0;
for(int i=0; i<r; ++i)
for(int j=0; j<c; ++j)
_max = max(_max, DP(i, j));
printf("%s: %d\n", name, _max);
}
return 0;
}