Advertisement
emodev

Untitled

Jan 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package f02_Matrices.Lab;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Arrays;
  7.  
  8. public class P08_WrongMeasurements {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         int[][] matrix = readMatrixFromConsole(reader);
  13.         int[][] copy = new int[matrix.length][matrix[0].length];
  14.  
  15.         for (int i = 0; i < matrix.length; i++) {
  16.             int[] arr = matrix[i];
  17.             copy[i] = arr;
  18.         }
  19.         String[] rowCol = reader.readLine().split(" ");
  20.         int row = Integer.parseInt(rowCol[0]);
  21.         int col = Integer.parseInt(rowCol[1]);
  22.  
  23.         int number = matrix[row][col];
  24.  
  25.         for (int i = 0; i < matrix.length; i++) {
  26.             for (int j = 0; j < matrix[0].length; j++) {
  27.                 int current = matrix[i][j];
  28.                 int result = 0;
  29.                 if (current == number) {
  30.                     if (i - 1 >= 0) {
  31.                         if (matrix[i - 1][j] != number) {
  32.                             result += matrix[i - 1][j];
  33.                         }
  34.                     }
  35.                     if (j - 1 >= 0) {
  36.                         if (matrix[i][j - 1] != number) {
  37.                             result += matrix[i][j - 1];
  38.                         }
  39.                     }
  40.                     if (i + 1 <= matrix.length - 1) {
  41.                         if (matrix[i + 1][j] != number) {
  42.                             result += matrix[i + 1][j];
  43.                         }
  44.                     }
  45.                     if (j + 1 <= matrix[0].length - 1) {
  46.                         if (matrix[i][j + 1] != number) {
  47.                             result += matrix[i][j + 1];
  48.                         }
  49.                     }
  50.                     copy[i][j] = result;
  51.                 }
  52.             }
  53.         }
  54.  
  55.  
  56.         for (int[] ints : copy) {
  57.             for (int anInt : ints) {
  58.                 System.out.print(anInt + " ");
  59.             }
  60.             System.out.println();
  61.         }
  62.  
  63.  
  64.     }
  65.  
  66.     private static int[][] readMatrixFromConsole(BufferedReader reader) throws IOException {
  67.         int row = Integer.parseInt(reader.readLine());
  68.  
  69.         int[][] matrix = new int[row][];
  70.  
  71.         for (int i = 0; i < row; i++) {
  72.             int[] array = Arrays.stream(reader.readLine().split(" "))
  73.                     .mapToInt(Integer::parseInt).toArray();
  74.             matrix[i] = array;
  75.  
  76.         }
  77.         return matrix;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement