sunfresh

Largest Surface

Jan 21st, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. Largest Surface
  2. Description
  3. Write a program that finds the largest possible sequence of equal neighboring elements in a rectangular matrix and prints its size.
  4.  
  5. Input
  6. On the first line you will receive the numbers N and M separated by a single space;
  7. On the next N lines there will be M numbers separated with spaces - the elements of the matrix;
  8. Output
  9. Print the size of the largest area of equal neighboring elements
  10. Constraints
  11. 3 <= N, M <= 1024
  12. Sample tests
  13. Input
  14. 5 6
  15. 1 3 2 2 2 4
  16. 3 3 3 2 4 4
  17. 4 3 1 2 3 3
  18. 4 3 1 3 3 1
  19. 4 3 3 3 1 1
  20. Output
  21. 13
  22. Hint: you can use the algorithm Depth-first search or Breadth-first search.
  23.  
  24.  
  25. package Recursion;
  26.  
  27. import java.util.Scanner;
  28.  
  29. public class LargestSurface {
  30. static int rows, cols;
  31. static int[][] matrix;
  32. static boolean[][] isVisited;
  33. static int maxSequence = 0;
  34.  
  35. public static void main(String[] args) {
  36. Scanner scanner = new Scanner(System.in);
  37. String inputLine = scanner.nextLine();
  38. String[] inputStringArr = inputLine.split(" ");
  39. rows = Integer.parseInt(inputStringArr[0]);
  40. cols = Integer.parseInt(inputStringArr[1]);
  41. matrix = new int[rows][cols];
  42. isVisited = new boolean[rows][cols];
  43.  
  44. for (int i = 0; i < rows; i++) {
  45. for (int j = 0; j < cols; j++) {
  46. matrix[i][j] = scanner.nextInt();
  47. }
  48. }
  49. System.out.println(getMaxSequence());
  50. }
  51. public static int calculateSurface(int[][] matrix, int row, int col, int currentNumber) {
  52. if (isValid(row, col)) {
  53. return 0;
  54. }
  55. if (isVisited(row, col, currentNumber)) {
  56. return 0;
  57. }
  58. int countNumbers = 1;
  59.  
  60. isVisited[row][col] = true;
  61. countNumbers += calculateSurface(matrix, row - 1, col, currentNumber);
  62. countNumbers += calculateSurface(matrix, row + 1, col, currentNumber);
  63. countNumbers += calculateSurface(matrix, row, col - 1, currentNumber);
  64. countNumbers += calculateSurface(matrix, row, col + 1, currentNumber);
  65. return countNumbers;
  66. }
  67.  
  68. public static boolean isValid(int row, int col) {
  69. return row < 0 || row >= rows || col < 0 || col >= cols;
  70. }
  71. public static boolean isVisited(int row, int col, int currentNumber) {
  72. return isVisited[row][col] || matrix[row][col] != currentNumber;
  73. }
  74. public static int getMaxSequence() {
  75. maxSequence = 0;
  76. for (int i = 0; i < rows; i++) {
  77. for (int j = 0; j < cols; j++) {
  78. if (!isVisited[i][j]) {
  79. int surface = calculateSurface(matrix, i, j, matrix[i][j]);
  80. maxSequence = Math.max(surface, maxSequence);
  81. }
  82. }
  83. }
  84. return maxSequence;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment