๋ฐฑ์ค - ๊ฐ์(Swift)
2022. 11. 29. 10:46ใAlgorithm
๋ฐฑ์ค - ๊ฐ์(Swift)
๋ฌธ์ ์ค๋ช
https://www.acmicpc.net/problem/15683
๋์ ํ์ด
์ฒ ์ ํ ๊ตฌํ๋ฌธ์ ์ด๊ณ ์ด ๋ถ์ ๋ธ๋ก๊ทธ๋ฅผ ๋ง์ด ์ฐธ๊ณ ํ๋ค.
https://0urtrees.tistory.com/227
ํ๋์ ์ขํ์์ ์์ผ๋ก ํผ์ง๋ ์ฌ๊ท๋ ์จ๋ดค์ง๋ง ์ด๋ ๊ฒ ๊ฐ๊ฐ์ ์ขํ์ ์๋ ๊ฒ๋ค์ ๋ํด ์ฌ๊ท๋ฅผ ํ๋ ๊ฒ์ ์ฒ์์ด์๋ค. ์ฐ์ cctv์ ์ขํ๋ฅผ cctv๋ฐฐ์ด์ ๋ฃ๊ณ DFS๋ฅผ ๋๋๋ง๋ค cctvIdx์ ํด๋นํ๋ ๊ฒ์ ๋นผ๊ณ ๋์ํ๋ฉด ๋๋ค.
๊ทธ๋ฆฌ๊ณ cctvList์ count๊ฐ DFS์ ๋ ๋ฒจ๊ณผ ๋์ผํด์ง๋ ํ์ฌ์ฌ๊ท๋ฅผ ์ข ๋ฃํ๋ฉด ๋๋ค.
import Foundation
typealias Tuple = (Int, Int, Int)
let dx = [-1, 0, 1, 0]
let dy = [0, 1, 0, -1]
let nm = readLine()!.components(separatedBy:" ").map{Int($0)!}
let N = nm[0], M = nm[1] // x, y์ขํ
var arr = [[Int]]()
var cctvList = [Tuple]() // cctv๋ฅผ ๋ด๊ณ ์๋ ๋ฐฐ์ด
var zeroCnt = 0 // ์ฒ์ 0์ ์ซ์
let posXRange = 0..<N
let posYRange = 0..<M
for _ in 0..<N {
arr.append(readLine()!.components(separatedBy:" ").map{Int($0)!})
}
for i in arr.indices {
for j in arr[i].indices {
if arr[i][j] >= 1 && arr[i][j] <= 5 { // cctv๋ค์ cctv๋ฐฐ์ด์ ์ถ๊ฐ
cctvList.append((arr[i][j], i, j))
} else if arr[i][j] == 0 {
zeroCnt += 1
}
}
}
// ํ ์ค์ ๋ฐฉํฅ์ ๋ง๊ฒ ์ฒ๋ฆฌํ๋ ํจ์
func coverZeroOneline(_ pos: (Int, Int), _ dir: (Int), _ tmpArr: inout [[Int]]) -> Int {
var posX = pos.0 + dx[dir]
var posY = pos.1 + dy[dir]
var coverZeroCnt = 0
while posXRange ~= posX && posYRange ~= posY && tmpArr[posX][posY] != 6 {
if tmpArr[posX][posY] == 0 {
tmpArr[posX][posY] = -1
coverZeroCnt += 1
}
posX = posX + dx[dir]
posY = posY + dy[dir]
}
return coverZeroCnt
}
// cctvํ์
์ ๋ฐ๋ผ ๋ผ์ธ์ ์ฒ๋ฆฌํ๋ ใ
๋ง์
func runCCTV(_ cctvType: Int, _ cctvPos: (Int, Int), _ dir: Int, _ tmpArr: inout [[Int]]) -> Int {
var coverToCnt = 0
if cctvType == 1 {
coverToCnt += coverZeroOneline(cctvPos, dir, &tmpArr)
} else if cctvType == 2 {
[0, 2].forEach { dirCnt in
coverToCnt += coverZeroOneline(cctvPos, (dir + dirCnt) % 4, &tmpArr)
}
} else if cctvType == 3 {
(0...1).forEach { dirCnt in
coverToCnt += coverZeroOneline(cctvPos, (dir + dirCnt) % 4, &tmpArr)
}
} else if cctvType == 4 {
(0...2).forEach { dirCnt in
coverToCnt += coverZeroOneline(cctvPos, (dir + dirCnt) % 4, &tmpArr)
}
} else if cctvType == 5 {
(0...3).forEach { dirCnt in
coverToCnt += coverZeroOneline(cctvPos, (dir + dirCnt) % 4, &tmpArr)
}
}
return coverToCnt
}
var Ans = Int.max
func DFS(_ cctvIdx: Int, _ coverCnt: Int, _ arr: [[Int]]) {
if cctvIdx >= cctvList.count {
let temp = zeroCnt - coverCnt
Ans = Ans > temp ? temp : Ans // ํ์ฌ ์ปค๋ฒํ๊ฒ ๋ ๋ง๋ค๋ฉด temp๊ฐ ์ ์ด์ง๋ฏ๋ก ์ ์ ๊ฐ์ด Ans๊ฐ ๋๋ค.
return
}
let newCCTV = cctvList[cctvIdx]
var tmpArr = arr
for dir in 0..<4 {
let cctvType = newCCTV.0
let cctvPos = (newCCTV.1, newCCTV.2)
let newCoverCnt = runCCTV(cctvType, cctvPos, dir, &tmpArr)
DFS(cctvIdx+1, coverCnt + newCoverCnt, tmpArr)
tmpArr = arr
}
}
DFS(0, 0, arr)
print(Ans)
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค - ๊ธฐ์ฐจ๊ฐ ์ด๋ ์ ํค์น๊ณ ์ํ์๋ฅผ(Swift) (0) | 2022.12.02 |
---|---|
๋ฐฑ์ค - ์๊ธฐ ์์ด(Swift) (0) | 2022.11.30 |
๋ฐฑ์ค - ๋๋๊ณค ์ปค๋ธ(Swift) (0) | 2022.11.28 |
๋ฐฑ์ค - ๋ฏธ์ธ๋จผ์ง ์๋ !(Swift) (0) | 2022.11.23 |
๋ฐฑ์ค - ์น์ฆ(Swift) (0) | 2022.11.22 |