深搜+记忆性dp
#include<bits/stdc++.h>
#define ll long long
const int mod = 1000000007;
using namespace std;
int r, c;
int dp[105][105];
int maze[105][105];
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
int dfs(int x, int y) {
int cnt = 1;
if(dp[x][y] != -1) {
return dp[x][y];
}
for(int i = 0; i < 4; ++i) {
int tx = dx[i] + x;
int ty = dy[i] + y;
if(tx >= 0 && tx < r && ty >= 0 && ty < c) {
if(maze[x][y] > maze[tx][ty]) {
cnt = max(cnt, dfs(tx, ty) + 1);
}
}
}
return dp[x][y] = cnt;
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &r, &c);
memset(dp, 0, sizeof(dp));
for(int i = 0; i < r; ++i) {
for(int j = 0; j < c; ++j) {
scanf("%d", &maze[i][j]);
dp[i][j] = -1;
}
}
int res = 0;
for(int i = 0; i < r; ++i) {
for(int j = 0; j < c; ++j) {
res = max(res, dfs(i, j));
}
}
printf("%lld\n", res);
}
return 0;
}