๐์ง์ ๋ฌธ์ ํ๋ฌ๊ฐ๊ธฐ
๋ฌธ์
์ง์์ง๋ ๋ธ๋ก์ ๋ช๊ฐ์ธ๊ฐ?
- 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;
}
}
}
}
}