mellowdeep

jedi galaxy

Jun 15th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3.  
  4.  
  5. public class JediGalaxy {
  6.  
  7.     public static int[][] galaxy;
  8.     public static int playerRow;
  9.     public static int playerCol;
  10.     public static int evilRow;
  11.     public static int evilCol;
  12.     public static long result = 0;
  13.    
  14.     public static void main(String[] args){
  15.        
  16.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));) {
  17.             String[] dimensions = reader.readLine().trim().split("\\s+");
  18.             int matrixRow = Integer.parseInt(dimensions[0]);
  19.             int matrixCol = Integer.parseInt(dimensions[1]);
  20.             galaxy = new int[matrixRow][matrixCol];
  21.             fillMatrix(galaxy);
  22.             String readerLine = reader.readLine();
  23.             while (!readerLine.equals("Let the Force be with you")) {
  24.                 String[] splitLine = readerLine.trim().split("\\s+");
  25.                 playerRow = Integer.parseInt(splitLine[0]);
  26.                 playerCol = Integer.parseInt(splitLine[1]);
  27.                 readerLine = reader.readLine();
  28.                 splitLine = readerLine.trim().split("\\s+");
  29.                 evilRow = Integer.parseInt(splitLine[0]);
  30.                 evilCol = Integer.parseInt(splitLine[1]);
  31.                 getPoints();
  32.                
  33.                 readerLine = reader.readLine();
  34.             }
  35.             System.out.println(result);
  36.         } catch (Exception e) {
  37.             e.printStackTrace();
  38.         }
  39.        
  40.     }
  41.  
  42.     private static void getPoints() {
  43.         for (int row = evilRow,col = evilCol; row >= 0; row--,col--) {
  44.                  if (isInside(galaxy,row,col)) {
  45.                      galaxy[row][col] = 0;
  46.                 }
  47.         }
  48.        
  49.         for (int row = playerRow,col = playerCol; row >= 0 ; row--,col++) {
  50.              if (isInside(galaxy,row,col)) {
  51.                  result += galaxy[row][col];
  52.              }
  53.         }
  54.     }
  55.  
  56.     private static boolean isInside(int[][] matrix, int row, int col) {
  57.         return row >= 0 && row < matrix.length && col >= 0 && col < matrix[row].length;
  58.     }
  59.  
  60.     private static void fillMatrix(int[][] matrix) {
  61.         int index = 0;
  62.         for (int row = 0; row < matrix.length; row++) {
  63.             for (int col = 0; col < matrix[row].length; col++) {
  64.                 matrix[row][col] = index++;
  65.             }
  66.         }
  67.        
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment