题意理解
这就是一个简单的记忆化搜索。
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int[] dx = {0, 0, -1, 1};
static int[] dy = {-1, 1, 0, 0};
public static class FastScanner {
private BufferedReader br;
private StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.valueOf(nextToken());
}
}
static int R;
static int C;
static int[][] cnt;
static int[][] height;
static boolean[][] visited;
public static void main(String[] args) {
FastScanner fs = new FastScanner();
R = fs.nextInt();
C = fs.nextInt();
cnt = new int[R][C];
height = new int[R][C];
visited = new boolean[R][C];
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cnt[i][j] = 1;
visited[i][j] = false;
height[i][j] = fs.nextInt();
}
}
int maxLen = -1;
for(int i = 0; i < R; i++) {
for(int j = 0; j < C; j++) {
maxLen = Math.max(maxLen, dfs(i, j));
}
}
System.out.println(maxLen);
}
public static int dfs(int x, int y) {
if(x < 0 || y < 0 || x >= R || y >= C) {
return 0;
}
if(visited[x][y]) {
return cnt[x][y];
}
visited[x][y] = true;
for(int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if(0 <= tx && tx < R && 0 <= ty && ty < C) {
if(height[x][y] > height[tx][ty]) {
cnt[x][y] = Math.max(cnt[x][y], dfs(tx, ty) + 1);
}
}
}
return cnt[x][y];
}
}