题目描述
blablabla
样例
blablabla
算法1
记忆搜索
Java 代码
import java.util.*;
import java.io.*;
public class Main{
static int[][] a,f;
static int R, C;
static int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter write = new PrintWriter(System.out);
public static void main(String[] args) throws IOException{
String[] s = read.readLine().split("\\s+");
R = Integer.parseInt(s[0]);
C = Integer.parseInt(s[1]);
a = new int[R][C];
f = new int[R+1][C+1];
for (int i = 0; i < R; i++) {
s = read.readLine().split("\\s+");
for (int j = 0; j < C; j++) {
a[i][j] = Integer.parseInt(s[j]);
}
}
int res = 0;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
res = Math.max(res, dp(i, j));
}
}
write.print(res);
write.flush();
write.close();
read.close();
}
static int dp(int x, int y) {
if (f[x][y] != 0) return f[x][y];
f[x][y] = 1;
for(int[] dir:dirs){
int xi = x + dir[0], yi = y + dir[1];
if (xi < R && xi >= 0 && yi < C && yi >= 0 && a[xi][yi] < a[x][y]) {
f[x][y] = Math.max(f[x][y], dp(xi, yi) + 1);
}
}
return f[x][y];
}
}