题意:给定一个整数矩阵,找到一条严格递减的最长路。
简单的动规题目,dp[i][j]表示走到(i, j)所走过的最长步数,向四个方向转移状态即可。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
string str; int r, c;
int a[maxn][maxn], dp[maxn][maxn];
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
inline bool in(const int x, const int y){
return x >= 0 && x < r && y >= 0 && y < c;
}
int solve(int x, int y){
if(dp[x][y] != -1) return dp[x][y];
int & ans = dp[x][y];
ans = 1;
for(int i = 0; i < 4; ++i){
int xx = x + dir[i][0], yy = y + dir[i][1];
if(in(xx, yy) && a[xx][yy] > a[x][y])
ans = max(ans, solve(xx, yy) + 1);
}
return ans;
}
int main()
{
int T; cin >> T;
while(T--){
memset(dp, -1, sizeof(dp));
cin >> str >> r >> c;
for(int i = 0; i < r; ++i)
for(int j = 0; j < c; ++j)
cin >> a[i][j];
int ans = INT_MIN;
for(int i = 0; i < r; ++i)
for(int j = 0; j < c; ++j)
ans = max(ans, solve(i, j));
printf("%s: %d\n", str.c_str(), ans);
}
return 0;
}