-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15686_치킨 배달.swift
64 lines (53 loc) · 1.49 KB
/
15686_치킨 배달.swift
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let input: [Int] = readLine()!
.split(separator: " ")
.map { Int($0)! }
let n = input[0]
let m = input[1]
var mapOfCity: [[Int]] = []
for _ in 0..<n {
let input: [Int] = readLine()!
.split(separator: " ")
.map { Int($0)! }
mapOfCity.append(input)
}
typealias Point = (r: Int, c: Int)
var pointsOfHouse: [Point] = []
var pointsOfChicken: [Point] = []
var answer: Int = 987654321
for r in 0..<n {
for c in 0..<n {
switch mapOfCity[r][c] {
case 1:
pointsOfHouse.append((r, c))
case 2:
pointsOfChicken.append((r, c))
default:
break
}
}
}
func countDistance(_ point1: Point, _ point2: Point) -> Int {
return abs(point1.r-point2.r) + abs(point1.c-point2.c)
}
func countChickenDistanceOfCity(_ selectedChickens: [Point]) -> Int {
var chickenDistanceOfCity: Int = 0
for pointOfHouse in pointsOfHouse {
var distance: Int = Int.max
for pointOfChicken in selectedChickens {
distance = min(distance, countDistance(pointOfHouse, pointOfChicken))
}
chickenDistanceOfCity += distance
}
return chickenDistanceOfCity
}
func bt(_ selectedChickens: [Point], _ index: Int) {
if selectedChickens.count == m {
answer = min(answer, countChickenDistanceOfCity(selectedChickens))
return
}
for i in index..<pointsOfChicken.count {
bt(selectedChickens+[pointsOfChicken[i]], i+1)
}
}
bt([], 0)
print(answer)