์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- Queue
- ios
- ๋ฐฑํธ๋ํน
- Swift
- BFS
- dp
- ๋นํธ์ฐ์ฐ์
- ์ฝ๋ฉํ ์คํธ
- ์ฝํ
- ์ด์งํ์
- ๋์ ํฉ
- ์์
- ๋ฐฑ์ค
- WebApp
- ์๊ณ ๋ฆฌ์ฆ
- ๋ถํ ์ ๋ณต
- ๋ถํ ํ์
- ๋ถํ ์ ๋ณต
- ๋ธ๋ฃจํธํฌ์ค
- ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ
- ์ ํด๋ฆฌ๋ ํธ์ ๋ฒ
- ๋ธ๋ฃจํธํฌ์ค ์๊ณ ๋ฆฌ์ฆ
- dfs
- ๋ฌธ์์ด
- ์คํ
- ํ๋ก๊ทธ๋๋จธ์ค
- WebView
- SwiftUI
Archives
- Today
- Total
MUKER_DEV with iOS
[swift] ํ๋ก๊ทธ๋๋จธ์ค - ์์ ์ง๋ ๋ณธ๋ฌธ
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๋ฌธ์ ํ์ด๋ฅผ ์ง๊ด์ ์ผ๋ก ์ดํดํ ์ ์๋๋ก ๊ณ ์ฐจํจ์๋ฅผ ์ฌ์ฉํ์ง ์์ ํ์ด์
๊ณ ์ฐจํจ์๋ฅผ ์ฌ์ฉํ ํ์ด๋ก ๋๋์ด ๊ณต๋ถํ๊ณ ์์ต๋๋ค.
๋์ ํ์ด
import Foundation
func solution(_ board:[[Int]]) -> Int {
var myboard = board
for (iIndex, iValue) in myboard.enumerated() {
for (jIndex, jValue) in iValue.enumerated() {
guard myboard != [[1]] else { return 0}
if jValue == 1 {
if jIndex == 0 {
myboard[iIndex][jIndex+1] = 1
if iIndex != 0 {
myboard[iIndex-1][jIndex] = 1
myboard[iIndex-1][jIndex+1] = 1
}
if iIndex != myboard.endIndex-1 {
myboard[iIndex+1][jIndex] = 1
myboard[iIndex+1][jIndex+1] = 1
}
continue
}
if jIndex == myboard[1].endIndex-1 {
myboard[iIndex][jIndex-1] = 1
if iIndex != 0 {
myboard[iIndex-1][jIndex] = 1
myboard[iIndex-1][jIndex-1] = 1
}
if iIndex != myboard.endIndex-1 {
myboard[iIndex+1][jIndex] = 1
myboard[iIndex+1][jIndex-1] = 1
}
continue
}
myboard[iIndex][jIndex+1] = 1
myboard[iIndex][jIndex-1] = 1
if iIndex != 0 {
myboard[iIndex-1][jIndex] = 1
myboard[iIndex-1][jIndex-1] = 1
myboard[iIndex-1][jIndex+1] = 1
}
if iIndex != myboard.endIndex-1 {
myboard[iIndex+1][jIndex] = 1
myboard[iIndex+1][jIndex-1] = 1
myboard[iIndex+1][jIndex+1] = 1
}
}
}
}
return myboard.flatMap { $0 }.filter { $0 == 0 }.count
}
- ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ ๋ค ์ด๊ฒ ์ต์ ์ด์์ด์...
- ๋์ ํ ์๊ฐํด๋ด๋ ์๋ฆ๋ค์ด ์๊ณ ๋ฆฌ์ฆ์ ์๊ฐ๋์ง ์์์ด๋๋ค..
- ์ค์ง 100์ ์ ์ํด ๋ง๋ค์ด์ง ๋ฐ๋ณด๊ฐ์ง๋ง ์ฐฉํ ๋ด ์ฝ๋^^
- ํ ์คํธ 9๋ฒ์ด n = 1์ด๋ผ์ [[1]]๊ฐ ๋ค์ด์ค๋๋ฐ ์ฐฉํ ๋ด์ฝ๋๋ ๊ทธ๋ฐ๊ฑฐ ๋ชฐ๋ผ ๋ฌด์์์ ์๋ฌ ๋ฑ์..
- ๊ทธ๋์ guard๋ฌธ์ผ๋ก [[1]] ์ ๊ฒฉ๋ฒค ๋๋ ค์คฌ์ต๋๋ค.
์ฐธ๊ณ ํ ๋งํ ํ์ด
import Foundation
func solution(_ board:[[Int]]) -> Int {
var results: [[Int]] = Array(repeating: Array(repeating: 0, count: board.count), count: board.count)
for (i,first) in board.enumerated() {
for (j,second) in first.enumerated() {
if board[i][j] == 1 {
for x in i-1...i+1 {
for y in j-1...j+1{
if 0 <= x && 0 <= y && x < board.count && y < board.count { results[x][y] = 1 }
}
}
}
}
}
return board.count * board.count - results.map{ $0.reduce(0,+) }.reduce(0, +)
}
import Foundation
func check_danger(_ b: inout [[Int]], _ N:Int, _ r:Int, _ c:Int) {
let RR = [-1, -1, -1, 0, 1, 1, 1, 0]
let CC = [-1, 0, 1, 1, 1, 0, -1, -1]
for d in 0..<8 {
if 0..<N ~= (r + RR[d]), 0..<N ~= (c + CC[d]) {
b[r + RR[d]][c + CC[d]] = 1
}
}
}
func solution(_ board:[[Int]]) -> Int {
var b = board
let N = b.count
for r in 0..<N {
for c in 0..<N {
if board[r][c] == 1 {
check_danger(&b, N, r, c)
}
}
}
return b.flatMap({$0}).filter{$0 == 0}.count
- ๋ค๋ฅธ์ฌ๋ ํ์ด ์ค ์ฐธ๊ณ ํ ๋งํ๊ฑฐ ๊ฐ์ ธ์์ต๋๋ค.
'๐ค ์๊ณ ๋ฆฌ์ฆ > programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[swift] ํ๋ก๊ทธ๋๋จธ์ค - k์ ๊ฐ์ (0) | 2022.12.31 |
---|---|
[swift] ํ๋ก๊ทธ๋๋จธ์ค - ์ธ๊ณ์ด ์ฌ์ (1) | 2022.12.30 |
[swift] ํ๋ก๊ทธ๋๋จธ์ค - ์จ์ด์๋ ์ซ์์ ๋ง์ (2) (0) | 2022.12.30 |
[swift] ํ๋ก๊ทธ๋๋จธ์ค - ์ฝ์์ ๊ฐ์์ ๋ง์ (0) | 2022.12.30 |
[swift] ํ๋ก๊ทธ๋๋จธ์ค - ๋ ์ ์ ์ฌ์ด์ ํฉ (0) | 2022.12.30 |