# 02/Swift - CTP
[Swift] 백준! 1012번 - 유기농 배추
장딴지연
2023. 1. 29. 03:32
반응형
https://www.acmicpc.net/problem/1012
1012번: 유기농 배추
차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에
www.acmicpc.net
import Foundation
let t = Int(readLine()!)!
for _ in 1...t {
var l = readLine()!.split(separator: " ").map{ Int(String($0))! }
let k = l[2]
var list:[[Int]] = Array.init(repeating: [0], count: k)
for i in 0...k-1 {
var l = readLine()!.split(separator: " ").map{ Int(String($0))! }
list[i] = ([l[0], l[1]])
}
var dic:[[Int]:Bool] = [:]
let dir = [[0,1],[0,-1],[1,0],[-1,0]]
var c = 0
for i in 0...k-1 {
if dic[[list[i][0], list[i][1]]] == nil {
dic[[list[i][0], list[i][1]]] = true
c += 1
setD(i)
}
}
func setD(_ i:Int) {
for d in dir {
let x = list[i][0]+d[0]
let y = list[i][1]+d[1]
let index = list.firstIndex(of: [x, y]) ?? -1
if index > -1 && dic[[list[index][0], list[index][1]]] == nil {
dic[[list[index][0], list[index][1]]] = true
setD(index)
}
}
}
print(c)
}
반응형