import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int[][]arr;
static boolean[][]vis;
static int n;
static int m;
static int ans = 0;
static int[]dx = {1,-1,0,0};
static int[]dy = {0,0,-1,1};
public static void main(String[] args) throws Exception{
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
m = scan.nextInt();
arr = new int[n][m];
vis = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = scan.nextInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dfs(i, j, 1);
}
}
System.out.println(ans);
}
private static void dfs(int i,int j,int cnt) {
ans = Math.max(cnt, ans);
//可以选择向上下左右四个方向
vis[i][j] = true;
for (int k = 0; k < 4; k++) {
int x = dx[k]+i;
int y = dy[k]+j;
if (x>=0&&y>=0&&x<n&&y<m&&!vis[x][y]&&arr[x][y]<arr[i][j]) {
dfs(x, y, cnt+1);
}
}
//回溯
vis[i][j] = false;
}
}
DFS求最长路径
最新推荐文章于 2024-08-22 21:24:01 发布