Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let [N, M] = gets().split(' ').map(Number);
- let matrix = Array.from({ length: N }, () => gets().split('').map(Number));
- let visited = Array.from({ length: N }, () => Array(M).fill(false));
- let result = [];
- // console.log(N, M, matrix, visited)
- const dfs = (row, col) => {
- if(row < 0 || row >= N || col < 0 || col >= M || visited[row][col] || matrix[row][col] !== 1){
- return 0;
- }
- visited[row][col] = true;
- let size = 1;
- size += dfs(row - 1, col); // up
- size += dfs(row + 1, col); // down
- size += dfs(row, col - 1); // left
- size += dfs(row, col + 1); // right
- return size;
- }
- for(let i = 0; i < N; i++){
- for(let j = 0; j < M; j++){
- if(matrix[i][j] === 1 && !visited[i][j]){
- const size = dfs(i, j);
- result.push(size);
- }
- }
- }
- result.sort((a, b) => b - a);
- for(let size of result){
- console.log(size)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement