1 5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
25
#include <stdio.h> #include <string.h> #define maxn 102 int a[maxn][maxn], dp[maxn][maxn]; //dp[i][j]保存i,j点开始的最长路径 int t, r, c; const int f[][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; bool check(int i, int j){ if(i > 0 && i <= r && j > 0 && j <= c) return 1; return 0; } int cal(int i, int j){ if(check(i, j) == 0) return 0; if(dp[i][j]) return dp[i][j]; int len = 1, maxlen = 1; for(int k = 0; k < 4; ++k){ if(a[i + f[k][0]][j + f[k][1]] < a[i][j]){ len = cal(i + f[k][0], j + f[k][1]) + 1; if(len > maxlen) maxlen = len; } } dp[i][j] = maxlen; return maxlen; } int main(){ int maxlen, temp; scanf("%d", &t); while(t--){ scanf("%d%d", &r, &c); for(int i = 1; i <= r; ++i) for(int j = 1; j <= c; ++j) scanf("%d", &a[i][j]); maxlen = 1; for(int i = 1; i <= r; ++i) for(int j = 1; j <= c; ++j){ if((temp = cal(i, j)) > maxlen) maxlen = temp; } printf("%d\n", maxlen); memset(dp, 0, sizeof(dp)); } return 0; }