Last active
September 4, 2018 13:15
-
-
Save almond-bongbong/9373439b7ab2c7c02c744882b667c424 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 입력으로 주어진 판 정보를 가지고 몇 개의 블록이 지워질지 출력하라. | |
// m n board answer | |
// 4 5 [CCBDE, AAADE, AAABF, CCBBF] 14 | |
// 6 6 [TTTANT, RRFACC, RRRFCC, TRRRAA, TTMMMF, TMMTTJ] 15 | |
function turn(m, n, board, answer) { | |
var removeItem = []; | |
for (var i = 1; i < m; i++) { | |
for (var j = 1; j < n; j++) { | |
var item = board[i][j]; | |
if (item === '0') continue; | |
var topLeft = board[i - 1][j - 1]; | |
var top = board[i - 1][j]; | |
var left = board[i][j - 1]; | |
if (item === topLeft && item === top && item === left) { | |
removeItem.push((i - 1) + ',' + (j - 1)); | |
removeItem.push((i - 1) + ',' + j); | |
removeItem.push(i + ',' + (j - 1)); | |
removeItem.push(i + ',' + j); | |
} | |
} | |
} | |
removeItem = removeItem.reduce((acc, cur) => { | |
if (acc.indexOf(cur) < 0) { | |
acc.push(cur); | |
} | |
return acc; | |
}, []); | |
if (removeItem.length > 0) { | |
answer += removeItem.length; | |
removeItem.forEach(i => { | |
var y = parseInt(i.split(',')[0], 10); | |
var x = parseInt(i.split(',')[1], 10); | |
board[y][x] = '0'; | |
}); | |
for (var i = 1; i < m; i++) { | |
for (var j = 0; j < n; j++) { | |
var flag = true; | |
var sY = i; | |
var sX = j; | |
while(sY > 0) { | |
if (board[sY][sX] === '0') { | |
board[sY][sX] = board[sY - 1][sX]; | |
board[sY - 1][sX] = '0'; | |
} | |
sY--; | |
} | |
} | |
} | |
answer = turn(m, n, board, answer); | |
} | |
return answer; | |
} | |
function solution(m, n, board) { | |
var answer = 0; | |
board = board.map(row => row.split('')); | |
answer = turn(m, n, board, answer); | |
return answer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment