2022. 11. 22. 16:26ใAlgorithm
๋ฐฑ์ค - ์น์ฆ(Swift)
๋ฌธ์ ์ค๋ช
https://www.acmicpc.net/problem/2636
๋์ ํ์ด
์ค์ค๋ก์ ๊ธฐ๋ณธ๊ธฐ๊ฐ ๋งค์ฐ๋งค์ฐ ๋ถ์กฑํ๋ค๊ณ ๋๋ ๋ฌธ์ ์๋ค.
๋ฌธ์ ๋ฅผ ํธ๋๋ฐ ์๋นํ ์ค๋์๊ฐ์ด ๊ฑธ๋ ธ๋๋ฐ ๊ทธ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค.
1. ๋ชจ๋ ๊ณต๊ธฐ์ค์ ๋ ธ์ถ๋ ์น์ฆ๊ฐ ์๋ ์ธ๋ถ๊ณต๊ธฐ์ ๋ ธ์ถ๋ ์น์ฆ๋ง์ด ๋ น๋๋ค๋ ์ฌ์ค
2. ์ธ๋ถ ๊ณต๊ธฐ์ ๋ด๋ถ๊ณต๊ธฐ๋ฅผ ์ด๋ป๊ฒ ๋๋์ง
3. ์ธ๋ถ๊ณต๊ธฐ์ ์ํด ๋ น์ ์น์ฆ ์ดํ์ ์๊ธด ๊ณต๊ธฐ์ ๋ํ ์ฒ๋ฆฌ.
๋ฌด์๋ณด๋ค ์ ์ผ ํฐ ์ด์ ๋ ๋ฌธ์ ๋ฅผ ์ฐ์ต๊ฒ ์๊ฐํ๊ธฐ ๋๋ฌธ์ธ ๊ฒ ๊ฐ๋ค.
๋ณด์๋ง์ ๊ทธ๋ฅ ์์ ํ์์ผ๋ก ์ธ๋ถ๋ง ์ฒดํฌํด์ ๋ น์ด๋ฉด ๋๊ฒ ๋ค๊ณ ์๊ฐํ๋๋ฐ ์๊ฐ๋ณด๋ค ๊ณ ๋ คํ ๊ฒ ๋ง์๋ ๊ฒ์ด๋ค.
ํนํ 3๋ฒ์ด ์กฐ๊ธ ์ฑ๊ฐ์ จ๋๋ฐ ์ด๋ BFS๋ฅผ ๋๋ฆด๋๋ง๋ค visited๋ฅผ ์ด๊ธฐํ์ํด์ผ๋ก์จ ์ฒ๋ฆฌ๋ฅผ ํ ์ ์์๋ค.
๊ทธ๋ฆฌ๊ณ ๋๋ ์ธ๋ถ์น์ฆ๋ก ๊ฒฐ์ ์ด ๋๋ฉด ์๋ก์ด ์ซ์๋ก ๋ฐ๊ฟ์ ์ฐจ๋ณ์ ์ ์ฃผ๊ฑฐ๋ ์น์ฆ๊ฐ ์๋ ๊ฒฝ์ฐ flag๋ฅผ ์ฒ๋ฆฌํ์ง ์๋ ๋ฑ์ ์ข ๋ ํท๊ฐ๋ฆด๋งํ ๊ฒ๋ค์ ์ถ๊ฐํ๋ คํ๋๋ฐ, ์ด๋ณด๋ค๋ ๋ฐฉ๋ฌธํ์์ฌ๋ถ๋ ๊ฐ๋ ฅํ ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ ์ด์ฉํด ์ฒ๋ฆฌํ๊ฑฐ๋ flag๋ ํจ์์ ๋ฆฌํด๊ฐ์ ๋ํด์ ์ฒ๋ฆฌํจ์ผ๋ก์จ ๊ฐ๊ฒฐํ๊ฒ ํํํ๋ ์ญ๋์ด ํ์ํ๋ค.
import Foundation
struct Point {
let x: Int
let y: Int
init(_ x: Int, _ y: Int) {
self.x = x
self.y = y
}
var nexts: [Point] {
return [
Point(x+1, y),
Point(x-1, y),
Point(x, y-1),
Point(x, y+1)
]
}
}
let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let row = input[0], col = input[1]
var map = [[Int]]()
var tempMap = [[Int]]()
var visited = [[Bool]](repeating: [Bool](repeating: false, count: col), count: row)
let rowRange = 0...row-1
let colRange = 0...col-1
var result = 0
var cheezeCnt: [Int] = []
for _ in 0..<row {
map.append(readLine()!.components(separatedBy:" ").map{Int($0)!})
}
var flag = false
while true {
visited = [[Bool]](repeating: [Bool](repeating: false, count: col), count: row)
tempMap = map
BFS(0, 0)
result += 1
findOuterCheeze()
// ๋ฐ๊นฅ์น์ฆ๊ฐ ์กด์ฌํ์ง์๋๋ค๋ฉด = ์น์ฆ๊ฐ ๋ค ๋จ์ด์ก๋ค๋ฉด
if !flag {
break
} else {
meltingCheeze()
map = tempMap
flag = false
}
}
print(result-1)
print(cheezeCnt[cheezeCnt.count-1])
// ์ฒ์ ๋ฐ๊นฅ๋ฒฝ๋ค์ 2๋ก ์ง์
func BFS(_ x: Int, _ y: Int) {
visited[x][y] = true
var queue: [Point] = [Point(x, y)]
while !queue.isEmpty {
let curr = queue.removeFirst()
for next in curr.nexts where rowRange ~= next.x && colRange ~= next.y {
if !visited[next.x][next.y], tempMap[next.x][next.y] == 0 {
visited[next.x][next.y] = true
queue.append(next)
}
}
}
}
// ๋ฐ๊นฅ ์น์ฆ๋ค์ ์ฐพ๋ ํจ์
func findOuterCheeze() {
for i in 0..<row-1 {
for j in 0..<col-1 {
let p = Point(i, j)
for next in p.nexts {
if tempMap[i][j] == 1, tempMap[next.x][next.y] == 0, visited[next.x][next.y] {
visited[i][j] = true
flag = true
break
}
}
}
}
}
// ์น์ฆ๋ฅผ ๋
น์ด๋ ํจ์
func meltingCheeze() {
var cnt = 0
for i in 0..<row-1 {
for j in 0..<col-1 {
if tempMap[i][j] == 1, visited[i][j] {
tempMap[i][j] = 0
cnt += 1
}
}
}
cheezeCnt.append(cnt)
}
๋ค๋ฅธ ์ฌ๋์ ํ์ด
์ด๋ถ์ ํ์ด๋ ๋์ ํ์ด๋ณด๋ค ํจ์ฌ ๊น๋ํ๋ค.
์ธ๋ถ์ ๊ณต๊ธฐ๋ฅผ true๋ก ํํ์ ํ ๋ ์น์ฆ์ธ ๋ถ๋ถ์ ํ์ ์๋ฃ๊ณ 0์ผ๋ก๋ง ๋ฐ๊ฟ์ผ๋ก์จ(๋ฐฉ๋ฌธํ์๋) ์น์ฆ์๋ ๊ณณ์ด ๊ณต๊ธฐ๋ก ๋ณํ ๋์ ๋ก์ง์ ์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์์๋ค.
๊ทธ๋ฆฌ๊ณ bfs๋ฅผ ํธ์ถํ ๋๋ง๋ค visited๋ฅผ ์ด๊ธฐํ์ํจ๋ค๋ ์ ๊ณผ tempArr์ ๋ง๋ค์ง์๊ณ cnt๋ฅผ ๋ฆฌํดํจ์ผ๋ก์จ ์ข ๋ฃ์์ ์ ๋ง๋๋ ๊ฒ๋ ์ข์๋ฐฉ๋ฒ์ด์๋ค.
import Foundation
let nm = readLine()!.split(separator: " ").map{Int(String($0))!}
let n = nm[0]
let m = nm[1]
var cheeseCnt = [Int]()
var cheeseBoard = [[Int]]()
var time = 0
var cnt = 0
let rowRange = 0...n-1
let colRange = 0...m-1
for _ in 0..<n {
cheeseBoard.append(readLine()!.split(separator: " ").map{ Int(String($0))! })
}
func bfs() -> Int {
var visited = Array(repeating: Array(repeating: false, count: m), count: n)
var queue = [(Int, Int)]()
queue.append((0, 0))
visited[0][0] = true
cnt = 0
while !queue.isEmpty {
let p = queue.removeFirst()
let curX = p.0
let curY = p.1
for next in [(curX+1, curY), (curX-1, curY), (curX, curY-1), (curX, curY+1)] {
if rowRange ~= next.0, colRange ~= next.1, !visited[next.0][next.1] {
if cheeseBoard[next.0][next.1] == 0 {
visited[next.0][next.1] = true
queue.append((next.0, next.1))
} else if cheeseBoard[next.0][next.1] == 1 {
visited[next.0][next.1] = true
cheeseBoard[next.0][next.1] = 0
cnt += 1
}
}
}
}
cheeseCnt.append(cnt)
return cnt
}
while true {
time += 1
cnt = bfs()
if cnt == 0 {
break
}
}
print(time-1)
print(cheeseCnt[cheeseCnt.count-2])
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค - ๋๋๊ณค ์ปค๋ธ(Swift) (0) | 2022.11.28 |
---|---|
๋ฐฑ์ค - ๋ฏธ์ธ๋จผ์ง ์๋ !(Swift) (0) | 2022.11.23 |
๋ฐฑ์ค - ํฑ๋๋ฐํด 2(Swift) (0) | 2022.11.21 |
๋ฐฑ์ค - ์ธ๊ตฌ์ด๋(Swift) (0) | 2022.11.21 |
๋ฐฑ์ค - ์นํจ๋ฐฐ๋ฌ(Swift) (0) | 2022.11.18 |