Vassil_Iliev

asdfggas

Jun 4th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const rowsCols = gets().split(' ');
  2. const rows = +rowsCols[0];
  3. const cols = +rowsCols[1];
  4.  
  5. const matrix = Array.from({ length: rows }).map(() => gets().split(' ').map(Number));
  6.  
  7.  
  8. let biggest = 0;
  9. let temp = [];
  10. temp[0] = 0;
  11.  
  12.  
  13. const areaRecursive = (m, rowsMax, colsMax, temp, seen, curRow, curCol) => {
  14.  
  15.     const left = curCol - 1 >= 0 ? matrix[curRow][curCol - 1] : null
  16.     const right = curCol + 1 < colsMax ? matrix[curRow][curCol + 1] : null
  17.     const up = curRow - 1 >= 0 ? matrix[curRow - 1][curCol] : null
  18.     const down = curRow + 1 < rowsMax ? matrix[curRow + 1][curCol] : null
  19.  
  20.     // if (left === null || right === null || up === null || down === null || seen[curRow][curCol]) {
  21.  
  22.     //     return;
  23.     // }
  24.  
  25.  
  26.     if (curRow < 0 || curCol < 0 || curRow >= matrix.length || curCol >= matrix[0].length || seen[curRow][curCol]) return;
  27.  
  28.     temp[0] += 1
  29.     seen[curRow][curCol] = true;
  30.    
  31.  
  32.     if (left === matrix[curRow][curCol] && left !== null) {
  33.  
  34.         areaRecursive(m, rowsMax, colsMax, temp, seen, curRow, curCol - 1)
  35.     }
  36.     if (right === matrix[curRow][curCol] && right !== null) {
  37.  
  38.         areaRecursive(m, rowsMax, colsMax, temp, seen, curRow, curCol + 1)
  39.     }
  40.     if (up === matrix[curRow][curCol] && up !== null) {
  41.  
  42.         areaRecursive(m, rowsMax, colsMax, temp, seen, curRow - 1, curCol)
  43.     }
  44.     if (down === matrix[curRow][curCol] && down !== null) {
  45.  
  46.         areaRecursive(m, rowsMax, colsMax, temp, seen, curRow + 1, curCol)
  47.     }
  48.  
  49.     return;
  50. };
  51.  
  52. const traversal = matrix => {
  53.  
  54.     const seen = Array.from({ length: rows }, () => []).map(() => Array.from({ length: cols }, () => false));
  55.  
  56.     for (let i = 0; i < matrix.length; i++) {
  57.         for (let j = 0; j < matrix[0].length; j++) {
  58.             if (seen[i][j] !== true) {
  59.                 areaRecursive(matrix, rows, cols, temp, seen, i, j)
  60.  
  61.                 if (temp[0] > biggest) {
  62.                     biggest = temp[0];
  63.                 }
  64.                 temp[0] = 0;
  65.             }
  66.         }
  67.     }
  68. }
  69.  
  70. traversal(matrix);
  71. print(biggest);
  72.  
Advertisement
Add Comment
Please, Sign In to add comment