강의로 돌아가기
Seyong Cho

테스트 7번만 실패하네요

어디가 잘못된건지 알수 있을까요?

function setCharAt(str,index,chr){
    return str.substr(0,index) + chr + str.substr(index+1);
}

function detectAndDestroy (board, count) {
    if(board.length < 2) 
        return 0

    let newCount = 0;
    let destroy = {}
    for(let y = 0; y < board.length - 1; y ++) {
        for(let x = 0; x < board.length - 1; x ++) {
            if(board[y].charAt(x) != '#') {
                if(board[y].charAt(x) == board[y].charAt(x+1) && 
                  board[y].charAt(x) == board[y+1].charAt(x) && 
                  board[y].charAt(x) == board[y+1].charAt(x+1)) {
                    destroy[y + "_" + x] = [y,x]
                    destroy[y + "_" + (x+1)] = [y,x + 1]
                    destroy[(y+1) + "_" + x] = [y + 1,x]
                    destroy[(y+1) + "_" + (x+1)] = [y+ 1,x + 1]
                }
            }
        }
    }

    for(let key in destroy) {
        let tile = destroy[key]
        board[tile[0]] = setCharAt(board[tile[0]], tile[1], '#')
        count ++
        newCount ++
    }

    board = moveDown(board);
    if(newCount > 0)
        return detectAndDestroy(board, count);

    return count;
}

function moveDown (board) {
    for(let y = board.length - 1; y > 0; y --) {
        for(let x = 0; x < board.length; x ++) {
            if(board[y].charAt(x) == '#') {
                let j = y - 1
                while(j > -1) {
                    if(board[j].charAt(x) != '#')
                        break;
                    j --
                }
                if(j > -1) {
                    board[y] = setCharAt(board[y], x, board[j].charAt(x))
                    board[j] = setCharAt(board[j], x, '#')
                }
            }
        }
    }
    return board;
}

function solution(m, n, board) {
    var answer = detectAndDestroy(board, 0);

    return answer;
}
작성중인 코드―solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function setCharAt(str,index,chr){
    return str.substr(0,index) + chr + str.substr(index+1);
}

function detectAndDestroy (board, count) {
    if(board.length < 2) 
        return 0

    let newCount = 0;
    let destroy = {}
    for(let y = 0; y < board.length - 1; y ++) {
        for(let x = 0; x < board.length - 1; x ++) {
            if(board[y].charAt(x) != '#') {
                if(board[y].charAt(x) == board[y].charAt(x+1) && 
                  board[y].charAt(x) == board[y+1].charAt(x) && 
                  board[y].charAt(x) == board[y+1].charAt(x+1)) {
                    destroy[y + "_" + x] = [y,x]
                    destroy[y + "_" + (x+1)] = [y,x + 1]
                    destroy[(y+1) + "_" + x] = [y + 1,x]
                    destroy[(y+1) + "_" + (x+1)] = [y+ 1,x + 1]
                }
            }
        }
    }

    for(let key in destroy) {
        let tile = destroy[key]
        board[tile[0]] = setCharAt(board[tile[0]], tile[1], '#')
        count ++
        newCount ++
    }

    board = moveDown(board);
    if(newCount > 0)
        return detectAndDestroy(board, count);

    return count;
}

function moveDown (board) {
    for(let y = board.length - 1; y > 0; y --) {
        for(let x = 0; x < board.length; x ++) {
            if(board[y].charAt(x) == '#') {
                let j = y - 1
                while(j > -1) {
                    if(board[j].charAt(x) != '#')
                        break;
                    j --
                }
                if(j > -1) {
                    board[y] = setCharAt(board[y], x, board[j].charAt(x))
                    board[j] = setCharAt(board[j], x, '#')
                }
            }
        }
    }
    return board;
}

function solution(m, n, board) {
    var answer = detectAndDestroy(board, 0);

    return answer;
}
0 개의 답변
답변 쓰기
이 입력폼은 마크다운 문법을 지원합니다. 마크다운 가이드 를 참고하세요.