Skip to content

Commit 6b71a02

Browse files
authored
[PGS] 17679 [1차] 프렌즈4블록 (Lv.2)
1 parent d09055d commit 6b71a02

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

박예진/5주차/260131.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// 4칸 같은지 확인
2+
function solve(x, y, board) {
3+
if (board[x][y] === '0') return false;
4+
5+
if (board[x][y] == board[x + 1][y] &&
6+
board[x + 1][y] == board[x][y + 1] &&
7+
board[x][y + 1] == board[x + 1][y + 1]) {
8+
return true;
9+
}
10+
return false;
11+
}
12+
13+
// 0이 아닌 부분까지 떨어지기
14+
function drop(m, n, board) {
15+
// 열 기준
16+
for(let j = 0; j < n; j++){
17+
// 행 기준
18+
let s = [];
19+
20+
// 아래 -> 위
21+
for(let i = m - 1; i >= 0; i--) {
22+
if (board[i][j] != '0') {
23+
s.push(board[i][j])
24+
}
25+
}
26+
// 다시 아래부터 채우기
27+
let r = m - 1;
28+
for(const num of s) {
29+
board[r][j] = num;
30+
r--;
31+
}
32+
// 위에 남은칸들
33+
for(let i = 0; i < m - s.length; i++) {
34+
board[i][j] = '0';
35+
}
36+
}
37+
}
38+
39+
function solution(m, n, board) {
40+
var answer = 0;
41+
42+
while(true) {
43+
let flag = false;
44+
let temp = board.map(row => [...row]); // 깊은 복사
45+
46+
for(let i = 0; i < m - 1; i++){
47+
for(let j = 0; j < n - 1; j++){
48+
// 4칸이 같으면 삭제, 기존 것으로 비교
49+
if (solve(i, j, board)) {
50+
for(let k = i; k < i + 2; k++){
51+
for(let q = j; q < j + 2; q++){
52+
if (temp[k][q] !== '0') answer++;
53+
temp[k][q] = '0';
54+
}
55+
}
56+
flag = true;
57+
}
58+
}
59+
}
60+
board = temp;
61+
drop(m, n, board);
62+
63+
if (!flag) break;
64+
}
65+
66+
return answer;
67+
}

0 commit comments

Comments
 (0)