[Programmers] 안전지대
2023. 11. 23. 14:16
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
'Test > Coding Tests' 카테고리의 다른 글
[Programmers] 평행 (0) | 2023.11.24 |
---|---|
[Programmers] 저주의 숫자 3 (1) | 2023.11.24 |
[Programmers] 숨어있는 숫자의 덧셈 (2) (0) | 2023.11.21 |
[Programmers] 최대값 만들기(2) (1) | 2023.11.21 |
[Programmers] 직사각형 넓이 구하기 (0) | 2023.11.20 |