Advertisement
vladimirVenkov

PortalsNotYetWorking

Jul 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Portali {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.         String[] inpStart = br.readLine().split(" ");
  9.         int startRow = Integer.parseInt(inpStart[0]);
  10.         int startCol = Integer.parseInt(inpStart[1]);
  11.         String[] inpSize = br.readLine().split(" ");
  12.         int rows = Integer.parseInt(inpSize[0]);
  13.         int cols = Integer.parseInt(inpSize[1]);
  14.  
  15.         int[][] matrix = new int[rows][cols];
  16.  
  17.         for (int i = 0; i < rows ; i++) {
  18.             String[] temp = br.readLine().split(" ");
  19.             for (int j = 0; j < cols ; j++) {
  20.                 if (temp[j].equals("#")) {
  21.                     matrix[i][j] = -1;
  22.                 } else {
  23.                     matrix[i][j] = Integer.parseInt(temp[j]);
  24.                 }
  25.             }
  26.         }
  27.         printMatrix(matrix);
  28.  
  29.         int[] maxi = new int[1];
  30.         boolean[][] visited = new boolean[rows][cols];
  31.         dfs(matrix, visited, maxi, startRow, startCol, 0, 0);
  32.         System.out.println(maxi[0]);
  33.     }
  34.  
  35.     private static void dfs(int[][] matrix, boolean[][] visited, int[] max, int row, int col, int current, int previousValue) {
  36.  
  37.         if (row < 0 || col < 0 || row >= matrix.length || col >= matrix[0].length) {
  38.             current -= previousValue;
  39.             max[0] = Math.max(max[0], current);
  40.             return;
  41.         }
  42.         int value = matrix[row][col];
  43.  
  44.         if (visited[row][col] || value < 0) {
  45.             current -= previousValue;
  46.             max[0] = Math.max(max[0], current);
  47.             return;
  48.         }
  49.  
  50.         current += value;
  51.         visited[row][col] = true;
  52.  
  53.         dfs(matrix, visited, max, row + value, col, current, value);
  54.  
  55.         dfs(matrix, visited, max, row - value, col, current, value);
  56.  
  57.         dfs(matrix, visited, max, row, col + value, current, value);
  58.  
  59.         dfs(matrix, visited, max, row, col - value, current, value);
  60.  
  61.         visited[row][col] = false;
  62.  
  63.         current -= previousValue;
  64.         max[0] = Math.max(max[0], current);
  65.  
  66.     }
  67.  
  68.     private static void printMatrix(int[][] matrix) {
  69.         System.out.println();
  70.         for (int i = 0; i < matrix.length; i++) {
  71.             for (int j = 0; j < matrix[0].length; j++) {
  72.                 System.out.print(matrix[i][j] + " ");
  73.             }
  74.             System.out.println();
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement