백준 10709 js

인지용·2024년 11월 2일
0

알고리즘

목록 보기
4/46
post-thumbnail

const input = require('fs')
    .readFileSync(process.platform === 'linux' ? '/dev/stdin' : './data.txt')
    .toString()
    .trim()
    .split('\n');

const [H, W] = input[0].split(" ").map(Number);
let map = new Array(H).fill(-1).map(() => new Array(W).fill(-1))
let minute = 1;

for(let i=0; i<H; i++){
    const row = input[i+1].split('');

    for(let j=0; j<W; j++){
        const isCloud = row[j] === 'c';
        const existsCloud = row.includes('c');
        const afterCloud = map[i][j-1] >= 0;

        if(isCloud) {
            map[i][j] = 0;
            minute = 1;
        }

        //행에 구름이 존재하고, 구름뒤에만 카운트
        if(!isCloud && existsCloud && afterCloud) {
            map[i][j] = minute;
            minute++;
        }
    }
}

for(let i=0; i<map.length; i++){
    console.log(map[i].join(" "))
}

이 문제를 풀면서 js는 [][] 이런식의 이차원배열 선언이 안된다는걸 처음 알았다.

그래서 1차원배열 선언 후 map으로 배열을 재선언해줘야한다.
근데 map을 돌려면 값이 있어야 한다.

그리고 분을 카운트 하기 위해서는 구름 존재유무와 구름전에 있는지 등 여러 플래그가 필요했다.

profile
한-줄

0개의 댓글