Advertisement
Ligh7_of_H3av3n

12. The Matrix

May 17th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package Uprajnenie;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5. import java.util.Scanner;
  6.  
  7. public class TheMatrix {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         int rows = scanner.nextInt();
  13.         int cols = scanner.nextInt();
  14.         scanner.nextLine(); // Consume newline
  15.  
  16.         char[][] matrix = new char[rows][cols];
  17.         for (int i = 0; i < rows; i++) {
  18.             String[] row = scanner.nextLine().split(" ");
  19.             for (int j = 0; j < cols; j++) {
  20.                 matrix[i][j] = row[j].charAt(0);
  21.             }
  22.         }
  23.  
  24.         char fillChar = scanner.next().charAt(0);
  25.         int startRow = scanner.nextInt();
  26.         int startCol = scanner.nextInt();
  27.  
  28.         fillMatrix(matrix, fillChar, startRow, startCol);
  29.  
  30.         // Print the filled matrix
  31.         for (int i = 0; i < rows; i++) {
  32.             for (int j = 0; j < cols; j++) {
  33.                 System.out.print(matrix[i][j]);
  34.             }
  35.             System.out.println();
  36.         }
  37.     }
  38.  
  39.     private static void fillMatrix(char[][] matrix, char fillChar, int startRow, int startCol) {
  40.         char startChar = matrix[startRow][startCol];
  41.         int rows = matrix.length;
  42.         int cols = matrix[0].length;
  43.  
  44.         Queue<Integer> queue = new LinkedList<>();
  45.         queue.add(startRow);
  46.         queue.add(startCol);
  47.  
  48.         while (!queue.isEmpty()) {
  49.             int row = queue.poll();
  50.             int col = queue.poll();
  51.  
  52.             if (matrix[row][col] != startChar) {
  53.                 continue;
  54.             }
  55.  
  56.             matrix[row][col] = fillChar;
  57.  
  58.             if (isValid(row - 1, col, rows, cols)) {
  59.                 queue.add(row - 1);
  60.                 queue.add(col);
  61.             }
  62.             if (isValid(row + 1, col, rows, cols)) {
  63.                 queue.add(row + 1);
  64.                 queue.add(col);
  65.             }
  66.             if (isValid(row, col - 1, rows, cols)) {
  67.                 queue.add(row);
  68.                 queue.add(col - 1);
  69.             }
  70.             if (isValid(row, col + 1, rows, cols)) {
  71.                 queue.add(row);
  72.                 queue.add(col + 1);
  73.             }
  74.         }
  75.     }
  76.  
  77.     private static boolean isValid(int row, int col, int rows, int cols) {
  78.         return row >= 0 && row < rows && col >= 0 && col < cols;
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement