DFS #7 RE 90分 求帮忙看看

P1434 [SHOI2002] 滑雪

momal @ 2021-12-17 13:16:21


import java.io.*;

public class Main {
    static int[][] dp;
    static int[][] map;
    static int row;
    static int col;

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String s;
        sequence:
        while ((s = bf.readLine()) != null) {
            String[] src = s.split(" ");
            row = Integer.parseInt(src[0]);
            col = Integer.parseInt(src[1]);
            dp = new int[row][col];
            map = new int[row][col];
            for (int i = 0; i < row; i++) {
                String[] seq = bf.readLine().split(" ");
                for (int j = 0; j < col; j++) {
                    map[i][j] = Integer.parseInt(seq[j]);
                    dp[i][j] = -1;
                }
            }

            int rst = 0;
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (dp[i][j] > -1) {
                        continue;
                    }
                    rst = Math.max(dfs(i, j),rst);
                }
            }
            System.out.println(rst);
        }
    }

    static int dfs(int i, int j) {
        int rst = 1;
        if (dp[i][j] > -1) {
            return dp[i][j];
        }
        if (i > 0 && map[i][j] > map[i - 1][j]) {
            rst = Math.max(dfs(i - 1, j) + 1, rst);
        }
        if (j > 0 && map[i][j] > map[i][j - 1]) {
            rst = Math.max(dfs(i, j - 1) + 1, rst);
        }
        if (i < row - 1 && map[i][j] > map[i + 1][j]) {
            rst = Math.max(dfs(i + 1, j) + 1, rst);
        }
        if (j < col - 1 && map[i][j] > map[i][j + 1]) {
            rst = Math.max(dfs(i, j + 1) + 1, rst);
        }
        dp[i][j] = rst;
        return rst;
    }

}

|