Didart

The Matrix

Jan 14th, 2023
899
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. package MultidimensionalArrays2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.nio.charset.StandardCharsets;
  7. import java.util.Arrays;
  8. import java.util.stream.Collectors;
  9.  
  10. public class TheMatrix {
  11.     public static void main(String[] args) throws IOException {
  12.  
  13.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
  14.  
  15.         int rows = Arrays
  16.                 .stream(reader.readLine().trim().split("\\s+"))
  17.                 .mapToInt(Integer::parseInt)
  18.                 .findFirst().orElse(0);
  19.  
  20.         char[][] matrix = reader
  21.                 .lines()
  22.                 .limit(rows)
  23.                 .map(line -> line.replaceAll("\\s+", "")
  24.                         .toCharArray())
  25.                 .toArray(char[][]::new);
  26.  
  27.         char fillChar = reader
  28.                 .readLine()
  29.                 .trim()
  30.                 .charAt(0);
  31.  
  32.         int[] startPosition = Arrays
  33.                 .stream(reader.readLine().trim().split("\\s+"))
  34.                 .mapToInt(Integer::parseInt)
  35.                 .toArray();
  36.  
  37.         int startRow = startPosition[0];
  38.         int startCol = startPosition[1];
  39.  
  40.         char startChar = matrix[startRow][startCol];
  41.  
  42.         fill(matrix, startRow, startCol, fillChar, startChar);
  43.  
  44.         System.out.println(Arrays.stream(matrix)
  45.                 .map(String::new)
  46.                 .collect(Collectors.joining(System.lineSeparator())));
  47.     }
  48.  
  49.     private static void fill(char[][] matrix, int row, int col, char fillChar, char startChar) {
  50.         if (matrix[row][col] != startChar) {
  51.             return;
  52.         }
  53.  
  54.         matrix[row][col] = fillChar;
  55.  
  56.         if (row + 1 < matrix.length) {
  57.             fill(matrix, row + 1, col, fillChar, startChar);
  58.         }
  59.  
  60.         if (row - 1 >= 0) {
  61.             fill(matrix, row - 1, col, fillChar, startChar);
  62.         }
  63.  
  64.         if (col + 1 < matrix[row].length) {
  65.             fill(matrix, row, col + 1, fillChar, startChar);
  66.         }
  67.  
  68.         if (col - 1 >= 0) {
  69.             fill(matrix, row, col - 1, fillChar, startChar);
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
Advertisement
Comments
  • PunkSheeo
    2 years
    # text 0.18 KB | 0 0
    1. th the the the th th the e mma-a-ama ama t tic ri amtier msterix??? matrix??? ๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿค‘๐Ÿซ ๐Ÿ—‘๏ธ๐Ÿ˜๐Ÿ—‘๏ธ๐Ÿ—‘๏ธ๐Ÿซ ๐Ÿคจ๐Ÿซ ๐Ÿ˜๐Ÿคจ
Add Comment
Please, Sign In to add comment