Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.advanced;
- import java.io.ByteArrayInputStream;
- import java.util.Arrays;
- import java.util.Scanner;
- public class LargestArea {
- static int rowsIn;
- static int colsIn;
- static int checkedPosition;
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int[] defineMatrix = Arrays.stream(input.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
- rowsIn = defineMatrix[0];
- colsIn = defineMatrix[1];
- int[][] matrix = new int[rowsIn][colsIn];
- for (int row = 0; row < rowsIn; row++) {
- for (int col = 0; col < colsIn; col++) {
- matrix[row][col] = Integer.parseInt(input.next());
- }
- }
- int longestPath = 0;
- int currentValue;
- checkedPosition = 1111;
- for (int rows = 0; rows < rowsIn; rows++) {
- for (int cols = 0; cols < colsIn; cols++) {
- if (matrix[rows][cols] != checkedPosition) {
- currentValue = matrix[rows][cols];
- int size = getPathSize(matrix, rows, cols, currentValue);
- longestPath = Math.max(size, longestPath);
- matrix[rows][cols] = checkedPosition;
- }
- }
- }
- System.out.println(longestPath);
- }
- private static int getPathSize(int[][] matrix, int rows, int cols, int currentValue) {
- if (!isInRange(matrix, rows, cols)) {
- return 0;
- }
- if (matrix[rows][cols] != currentValue) {
- return 0;
- }
- matrix[rows][cols] = checkedPosition;
- int counter = 1;
- for (int r = rows - 1; r <= rows + 1; r++) {
- for (int c = cols - 1; c <= cols + 1; c++) {
- if (r != rows || c != cols)
- counter += getPathSize(matrix, r, c, currentValue);
- }
- }
- return counter;
- }
- private static boolean isInRange(int[][] matrix, int rows, int cols) {
- return rows >= 0 && rows < rowsIn && cols >= 0 && cols < colsIn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement