Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const rowsCols = gets().split(' ');
- const rows = +rowsCols[0];
- const cols = +rowsCols[1];
- const matrix = Array.from({ length: rows }).map(() => gets().split(' ').map(Number));
- let biggest = 0;
- let temp = [];
- temp[0] = 0;
- const areaRecursive = (m, rowsMax, colsMax, temp, seen, curRow, curCol) => {
- const left = curCol - 1 >= 0 ? matrix[curRow][curCol - 1] : null
- const right = curCol + 1 < colsMax ? matrix[curRow][curCol + 1] : null
- const up = curRow - 1 >= 0 ? matrix[curRow - 1][curCol] : null
- const down = curRow + 1 < rowsMax ? matrix[curRow + 1][curCol] : null
- // if (left === null || right === null || up === null || down === null || seen[curRow][curCol]) {
- // return;
- // }
- if (curRow < 0 || curCol < 0 || curRow >= matrix.length || curCol >= matrix[0].length || seen[curRow][curCol]) return;
- temp[0] += 1
- seen[curRow][curCol] = true;
- if (left === matrix[curRow][curCol] && left !== null) {
- areaRecursive(m, rowsMax, colsMax, temp, seen, curRow, curCol - 1)
- }
- if (right === matrix[curRow][curCol] && right !== null) {
- areaRecursive(m, rowsMax, colsMax, temp, seen, curRow, curCol + 1)
- }
- if (up === matrix[curRow][curCol] && up !== null) {
- areaRecursive(m, rowsMax, colsMax, temp, seen, curRow - 1, curCol)
- }
- if (down === matrix[curRow][curCol] && down !== null) {
- areaRecursive(m, rowsMax, colsMax, temp, seen, curRow + 1, curCol)
- }
- return;
- };
- const traversal = matrix => {
- const seen = Array.from({ length: rows }, () => []).map(() => Array.from({ length: cols }, () => false));
- for (let i = 0; i < matrix.length; i++) {
- for (let j = 0; j < matrix[0].length; j++) {
- if (seen[i][j] !== true) {
- areaRecursive(matrix, rows, cols, temp, seen, i, j)
- if (temp[0] > biggest) {
- biggest = temp[0];
- }
- temp[0] = 0;
- }
- }
- }
- }
- traversal(matrix);
- print(biggest);
Advertisement
Add Comment
Please, Sign In to add comment