Untitled

 avatar
unknown
plain_text
4 days ago
1.4 kB
3
Indexable
static int maxcc = 0;

public static void cpi(int row, int col, int[][] arr, int ccsf) {
    if (row < 0 || row >= arr.length || col < 0 || col >= arr[0].length || arr[row][col] == -1) {
        return;
    }

    if (row == arr.length - 1 && col == arr[0].length - 1) {
        helper(row, col, arr, ccsf);
        return;
    }

    int cherries = arr[row][col];
    arr[row][col] = 0;
    cpi(row, col + 1, arr, ccsf + cherries);
    cpi(row + 1, col, arr, ccsf + cherries);
    arr[row][col] = cherries;
}

public static void helper(int row, int col, int[][] arr, int ccsf) {
    if (row < 0 || row >= arr.length || col < 0 || col >= arr[0].length || arr[row][col] == -1) {
        return;
    }

    if (row == 0 && col == 0) {
        maxcc = Math.max(maxcc, ccsf);
        return;
    }

    int cherries = arr[row][col];
    arr[row][col] = 0;
    helper(row, col - 1, arr, ccsf + cherries);
    helper(row - 1, col, arr, ccsf + cherries);
    arr[row][col] = cherries;
}

public static void main(String[] args) {
    Scanner scn = new Scanner(System.in);
    int n = scn.nextInt();

    int[][] arr = new int[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            arr[i][j] = scn.nextInt();
        }
    }

    cpi(0, 0, arr, 0);
    System.out.println(maxcc);
}
Editor is loading...
Leave a Comment