Advertisement
Guest User

Untitled

a guest
Jun 4th, 2020
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.Arrays;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Pr12TheMatrix {
  9.  
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
  12.  
  13.         int rows = Arrays
  14.                 .stream(reader.readLine().trim().split("\\s+"))
  15.                 .mapToInt(Integer::parseInt)
  16.                 .findFirst().orElse(0);
  17.  
  18.         char[][] matrix = reader.lines()
  19.                 .limit(rows)
  20.                 .map(line -> line.replaceAll("\\s+", "").toCharArray())
  21.                 .toArray(char[][]::new);
  22.  
  23.         char fillChar = reader.readLine().trim().charAt(0);
  24.  
  25.         int[] startPosition = Arrays
  26.                 .stream(reader.readLine().trim().split("\\s+"))
  27.                 .mapToInt(Integer::parseInt)
  28.                 .toArray();
  29.  
  30.         int startRow = startPosition[0];
  31.         int startCol = startPosition[1];
  32.  
  33.         char startChar = matrix[startRow][startCol];
  34.  
  35.         fill(matrix, startRow, startCol, fillChar, startChar);
  36.  
  37.         System.out.println(Arrays.stream(matrix)
  38.                 .map(String::new)
  39.                 .collect(Collectors.joining(System.lineSeparator())));
  40.     }
  41.  
  42.     private static void fill(char[][] matrix, int row, int col, char fillChar, char startChar) {
  43.         if (matrix[row][col] != startChar) {
  44.             return;
  45.         }
  46.  
  47.         matrix[row][col] = fillChar;
  48.  
  49.         if (row + 1 < matrix.length) {
  50.             fill(matrix, row + 1, col, fillChar, startChar);
  51.         }
  52.  
  53.         if (row - 1 >= 0) {
  54.             fill(matrix, row - 1, col, fillChar, startChar);
  55.         }
  56.  
  57.         if (col + 1 < matrix[row].length) {
  58.             fill(matrix, row, col + 1, fillChar, startChar);
  59.         }
  60.  
  61.         if (col - 1 >= 0) {
  62.             fill(matrix, row, col - 1, fillChar, startChar);
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement