Ivelin_1936

Crossfire

Jun 8th, 2018
5,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Crossfire {
  6.  
  7.     private static final int DESTROY = 0;
  8.     private static final String END_COMMAND = "Nuke it from orbit";
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         int[][] matrix = fillTheMatrix(reader);
  14.  
  15.         String line;
  16.         while (!END_COMMAND.equalsIgnoreCase(line = reader.readLine())) {
  17.             String[] tokens = line.split("\\s+");
  18.  
  19.             int x = Integer.parseInt(tokens[0]);
  20.             int y = Integer.parseInt(tokens[1]);
  21.             int radius = Integer.parseInt(tokens[2]);
  22.  
  23.             //Destroy all cells in the given radius (matrix[x][y] = 0;)
  24.             destroyAfectedColumsCellsInTheGivenRadius(matrix, x, y, radius);
  25.             destroyAfectedRowsCellsInTheGivenRadius(matrix, x, y, radius);
  26.  
  27.             //Deleting all cells which are destroyed (matrix[x][y] = 0;)
  28.             deleteZeroedCells(matrix);
  29.         }
  30.         printTheMatrix(matrix);
  31.     }
  32.  
  33.     private static void destroyAfectedRowsCellsInTheGivenRadius(int[][] matrix, int x, int y, int radius) {
  34.         int startRow = x - radius;
  35.         if (startRow < 0)
  36.             startRow = 0;
  37.  
  38.         int endRow = x + radius;
  39.         if (endRow > matrix.length - 1)
  40.             endRow = matrix.length - 1;
  41.  
  42.         for (int r = startRow; r <= endRow; r++) {
  43.             try {
  44.                 matrix[r][y] = DESTROY;
  45.             } catch (IndexOutOfBoundsException ignored) {}
  46.         }
  47.     }
  48.  
  49.     private static void destroyAfectedColumsCellsInTheGivenRadius(int[][] matrix, int x, int y, int radius) {
  50.         int startCol = y - radius;
  51.         if (startCol < 0)
  52.             startCol = 0;
  53.  
  54.         int endCol = y + radius;
  55.         if (endCol > matrix[0].length - 1)
  56.             endCol = matrix[0].length - 1;
  57.  
  58.         for (int c = startCol; c <= endCol; c++) {
  59.             try {
  60.                 matrix[x][c] = DESTROY;
  61.             } catch (IndexOutOfBoundsException ignored) {}
  62.         }
  63.     }
  64.  
  65.     private static void deleteZeroedCells(int[][] matrix) {
  66.         for (int row = 0; row < matrix.length; row++) {
  67.             for (int col = 0; col < matrix[0].length; col++) {
  68.                 if (matrix[row][col] == 0) {
  69.                     moveAllRowNumbersOnLeft(matrix, row, col);
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     private static void moveAllRowNumbersOnLeft(int[][] matrix, int row, int col) {
  76.         for (int i = col; i < matrix[0].length; i++) {
  77.             try {
  78.                 matrix[row][i] = matrix[row][i + 1];
  79.             } catch (IndexOutOfBoundsException ioobe) {
  80.                 matrix[row][i] = 0;
  81.             }
  82.         }
  83.         //EXAMPLE
  84.         /**coming matrix  -->  returned matrix
  85.          * 1  2  0  4  5  -->  1  2  4  5  0
  86.          * 6  0  0  0 10  -->  6 10  0  0  0
  87.          * 11 12 0 14 15  -->  11 12 14 15 0*/
  88.     }
  89.  
  90.     private static int[][] fillTheMatrix(BufferedReader reader) throws IOException {
  91.         String[] dimensions = reader.readLine().split("\\s+");
  92.         int rowsCount = Integer.parseInt(dimensions[0]);
  93.         int colsCount = Integer.parseInt(dimensions[1]);
  94.  
  95.         int num = 1;
  96.         int[][] matrix = new int[rowsCount][colsCount];
  97.         for (int row = 0; row < rowsCount; row++) {
  98.             for (int col = 0; col < colsCount; col++) {
  99.                 matrix[row][col] = num;
  100.                 num++;
  101.             }
  102.         }
  103.         return matrix;
  104.     }
  105.  
  106.     private static void printTheMatrix(int[][] matrix) {
  107.         /**Printing the matrix, without printing cells equals to 0*/
  108.         for (int[] ints : matrix) {
  109.             boolean emptyRow = true;
  110.             for (int number : ints) {
  111.                 if (number != 0) {
  112.                     System.out.print(number + " ");
  113.                     emptyRow = false;
  114.                 }
  115.             }
  116.             /**If all cols on the current row are equals to 0,
  117.              * don't printing empty line*/
  118.             if (!emptyRow) {
  119.                 System.out.println();
  120.             }
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment