새소식

Test/Coding Tests

[Programmers] 안전지대

  • -

func solution(_ board:[[Int]]) -> Int {
    
  var copy = board
    for i in 0...board.count-1 {
        for j in 0...board.count-1 {
            if board[i][j] == 1 {
                if i != 0 && j != 0 {
                    if copy[i-1][j-1] != 1 {
                        copy[i-1][j-1] = 2
                    }
                }
                if i != 0 {
                    if copy[i-1][j] != 1 {
                        copy[i-1][j] = 2
                    }
                }
                if i != 0 && j+1 < board.count {
                    if copy[i-1][j+1] != 1 {
                        copy[i-1][j+1] = 2
                    }
                }
                if j != 0 {
                    if copy[i][j-1] != 1 {
                        copy[i][j-1] = 2
                    }
                }
                if j+1 < board.count {
                    if copy[i][j+1] != 1 {
                        copy[i][j+1] = 2
                    }
                }
                if i+1 < board.count && j != 0 {
                    if copy[i+1][j-1] != 1 {
                        copy[i+1][j-1] = 2
                    }
                }
                if i+1 < board.count {
                    if copy[i+1][j] != 1 {
                        copy[i+1][j] = 2
                    }
                }
                if i+1 < board.count && j+1 < board.count {
                    if copy[i+1][j+1] != 1 {
                        copy[i+1][j+1] = 2
                    }
                }
            }
        }
    }
    var result:Int = 0
    for i in 0...board.count-1 {
        result += copy[i].filter { $0==0 }.count
    }
    return result
}

 

출처

 

[Swift] 코딩테스트 연습! Lv0. 안전지대

/* 안전지대 - 다음 그림과 같이 지뢰가 있는 지역과 지뢰에 인접한 위, 아래, 좌, 우 대각선 칸을 모두 위험지역으로 분류합니다. 지뢰는 2차원 배열 board에 1로 표시되어 있고 board에는 지뢰가 매

zoiworld.tistory.com

 

728x90
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.