原题链接:https://vjudge.net/problem/UVA-10285
分类:记忆化搜索
备注:水题
#include <bits/stdc++.h>
using namespace std;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
char s[105];
int t, row, col, g[105][105], d[105][105];
bool ok(int r, int c) {
return r >=1 && r <= row && c >= 1 && c <= col;
}
int dfs(int r, int c) {
if (d[r][c]) return d[r][c];
d[r][c] = 1;
for (int i = 0; i < 4; i++) {
int rr = r + dir[i][0];
int cc = c + dir[i][1];
if (ok(rr, cc) && g[rr][cc] < g[r][c]) {
d[r][c] = max(d[r][c], dfs(rr, cc) + 1);
}
}
return d[r][c];
}
int main(void) {
// freopen("in.txt", "r", stdin);
scanf("%d", &t);
while (t--) {
scanf("%s%d%d", s, &row, &col);
for (int i = 1; i <= row; i++)
for (int j = 1; j <= col; j++)
scanf("%d", &g[i][j]);
memset(d, 0, sizeof(d));
int ans = 0;
for (int i = 1; i <= row; i++)
for (int j = 1; j <= col; j++)
if (!d[i][j])
ans = max(ans, dfs(i, j));
printf("%s: %d\n", s, ans);
}
return 0;
}