这道题可以定义状态 f[i, j] 为从 (i, j) 出发所能滑的最大长度,则当前点只与四周点的最大长度有关,即得状态转移方程。
1 # include <stdio.h>
2 # include <memory.h>
3
4 # define MAX(x,y) ((x)>(y) ? (x):(y))
5
6 int r, c;
7 char name[21];
8 int f[101][101];
9 int h[101][101];
10
11 int dp(int i, int j);
12
13 int main()
14 {
15 int T, i, j, maxR;
16
17 scanf("%d", &T);
18 while (T--)
19 {
20 scanf("%s%d%d", name, &r, &c);
21 for ( i = 1; i <= r; ++i)
22 for ( j = 1; j <= c; ++j)
23 scanf("%d", &h[i][j]);
24
25 memset(f, 0, sizeof(f));
26
27 maxR = 1;
28 for ( i = 1; i <= r; ++i)
29 for ( j = 1; j <= c; ++j)
30 {
31 dp(i, j);
32 if (f[i][j] > maxR) maxR = f[i][j];
33 }
34
35 printf("%s: %d\n", name, maxR);
36 }
37
38 return 0;
39 }
40
41 int dp(int i, int j)
42 {
43 if (f[i][j] > 0) return f[i][j];
44 f[i][j] = 1;
45 if (j > 1 && h[i][j] > h[i][j-1]) f[i][j] = MAX(f[i][j], dp(i, j-1)+1);
46 if (j < c && h[i][j] > h[i][j+1]) f[i][j] = MAX(f[i][j], dp(i, j+1)+1);
47 if (i > 1 && h[i][j] > h[i-1][j]) f[i][j] = MAX(f[i][j], dp(i-1, j)+1);
48 if (i < r && h[i][j] > h[i+1][j]) f[i][j] = MAX(f[i][j], dp(i+1, j)+1);
49 return f[i][j];
50 }