-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ_마피아.swift
73 lines (64 loc) · 1.41 KB
/
BOJ_마피아.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
65
66
67
68
69
70
71
72
73
// 조건 분기가 포함된 state dfs
import Foundation
let N = Int(readLine()!)!
var points = readLine()!.split(separator: " ").map { Int($0)! }
var R: [[Int]] = []
for _ in 0..<N {
R.append(readLine()!.split(separator: " ").map { Int($0)! })
}
let me = Int(readLine()!)!
var alive = Array(repeating: true, count: N)
func aliveCount() -> Int {
return alive.filter { $0 }.count
}
func brightDie() -> Int {
var die = -1
var maxPoint = -1
for i in 0..<N where alive[i] {
if points[i] > maxPoint {
die = i
maxPoint = points[i]
}
}
return die
}
func canKill() -> [Int] {
return (0..<N).filter { alive[$0] && $0 != me }
}
func updatePoints(_ i: Int) {
for j in 0..<N where i != j {
points[j] += R[i][j]
}
}
func downdatePoints(_ i: Int) {
for j in 0..<N where i != j {
points[j] -= R[i][j]
}
}
var ans = 0
func dfs(_ night: Int) {
if !alive[me] {
ans = max(ans, night)
if ans == N / 2 {
print(ans)
exit(0)
}
return
}
if aliveCount() % 2 == 0 {
for i in canKill() {
alive[i] = false
updatePoints(i)
dfs(night + 1)
downdatePoints(i)
alive[i] = true
}
} else {
let die = brightDie()
alive[die] = false
dfs(night)
alive[die] = true
}
}
dfs(0)
print(ans)