๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿบ ๋งค์ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

2018 ์นด์นด์˜ค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ 1์ฐจ ํ”„๋ Œ์ฆˆ4๋ธ”๋ก JS ๋ฌธ์ œํ’€์ด

๐Ÿ”—์ง์ ‘ ๋ฌธ์ œ ํ’€๋Ÿฌ๊ฐ€๊ธฐ

 

 

๋ฌธ์ œ

 

์ง€์›Œ์ง€๋Š” ๋ธ”๋ก์€ ๋ช‡๊ฐœ์ธ๊ฐ€?

  • 2 x 2 ํ˜•ํƒœ๋กœ ๊ฐ™์€ ์บ๋ฆญํ„ฐ๊ฐ€ ๋ถ™์–ด์žˆ์œผ๋ฉด ์‚ฌ๋ผ์ง€๋ฉด์„œ ์ ์ˆ˜๋ฅผ ์–ป๋Š”๋‹ค.
  • ํ•œ ๋ธ”๋ก์€ ์—ฌ๋Ÿฌ๊ฐœ์˜ 2 x 2 ์— ํฌํ•จ๋  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ธ”๋ก์ด ์ง€์›Œ์ง„ ๋’ค์—๋Š” ์œ„์—์„œ ์•„๋ž˜๋กœ ๋ธ”๋ก๋“ค์ด ์Ÿ์•„์ง„๋‹ค.
  • ๋ผ์ด์–ธ(R), ๋ฌด์ง€(M), ์–ดํ”ผ์น˜(A), ํ”„๋กœ๋„(F), ๋„ค์˜ค(N), ํŠœ๋ธŒ(T), ์ œ์ด์ง€(J), ์ฝ˜(C)

 

 

 

๋ฌธ์ œํ’€์ด

 

function solution(m, n, board) {
    board = board.map(line => line.split(''));
    let pang = new Set();
    while (true) {
        for (let i = 0; i + 1 < m; i++) {
            for (let j = 0; j + 1 < n; j++) {
                if (!board[i][j]) {
                    continue;
                }
                getPang(i, j);
            }
        }
        if (!pang.size) {
            break;
        }
        del([...pang].map(e => e.split(',')));
        pang.clear();
        fall();
    }
    return board.reduce(
        (cnt, row) => cnt + row.reduce(
            (acc, cur) => !cur ? acc + 1 : acc, 0)
        , 0);
    
    function getPang(i, j) {
        if (board[i][j] === board[i+1][j] &&
            board[i][j] === board[i][j+1] &&
            board[i][j] === board[i+1][j+1]) {
            pang.add([i,j].join());
            pang.add([i+1,j].join());
            pang.add([i,j+1].join());
            pang.add([i+1,j+1].join());
        }
    }
    
    function del(panged) {
        while (panged.length) {
            const [x, y] = panged.pop();
            board[x][y] = 0;
        }
    }
    
    function fall() {
        for (let j = 0; j < n; j++) {
            let y = board.length - 1;
            for (let i = m - 1; 0 < i; i--) {
                if (!board[i][j] && board[i - 1][j]) {
                    board[y--][j] = board[i - 1][j];
                    board[i - 1][j] = 0;
                }
                if (board[i][j] && !board[i - 1][j]) {
                    y = i - 1;
                }
            }
        }
    }
}