import java.util.Scanner; public class main { //四个方向 static int p[][] = { {1, 0}, {0, 1},{-1, 0}, {0, -1}}; static int max = Integer.MIN_VALUE, r, c; public static void main(String[] args) { Scanner sc = new Scanner(System.in); r = sc.nextInt(); c = sc.nextInt(); int[][] a = new int[r][c]; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { a[i][j] = sc.nextInt(); } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { dfs(a, i, j, 1); } } System.out.println(max); } public static void dfs(int[][] a, int i, int j, int sum) { int t, x, y; if (sum > max)//找出可以走的路径中的最大值 max = sum; for (t = 0; t < 4; t++)//向上下左右分别搜索 { x = i + p[t][0];//在节点处向上和下移动 y = j + p[t][1];//在节点处向左和右移动 if (x >= 0 && x < r && y >= 0 && y < c && a[x][y] < a[i][j])//如果没有超出数组边界且这个节点比原来的小 { sum++;//我们的路的长度加一 dfs(a, x, y, sum);//在新节点重复此次循环,到达路径最小处结束循环 sum--;//回溯到可以走的节点时把走过的路的长度减去 } } } } /* F:\temp\21425366.618876\Main.java:6: class main is public, should be declared in a file named main.java public class main { ^ 1 error */ |
Double click to view unformatted code.