-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14502_연구소.swift
79 lines (64 loc) · 1.51 KB
/
14502_연구소.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
74
75
76
77
78
79
let size: [Int] = readLine()!
.split(separator: " ")
.map { Int($0)! }
let row: Int = size[0]
let col: Int = size[1]
var map: [[Int]] = []
let dx: [Int] = [-1, 1, 0, 0]
let dy: [Int] = [0, 0, -1, 1]
var answer: Int = -1
for _ in 0..<row {
let row: [Int] = readLine()!
.split(separator: " ")
.map { Int($0)! }
map.append(row)
}
bt(0)
print(answer)
func bt(_ wallCount: Int) {
if wallCount == 3 {
bfs()
return
}
for i in 0..<row {
for j in 0..<col {
if map[i][j] == 0 {
map[i][j] = 1
bt(wallCount + 1)
map[i][j] = 0
}
}
}
}
func bfs() {
var map: [[Int]] = map
var queue: [(Int, Int)] = []
for i in 0..<row {
for j in 0..<col {
if map[i][j] == 2 {
queue.append((i, j))
}
}
}
while !queue.isEmpty {
let current = queue.removeFirst()
for i in dx.indices {
let nextX: Int = dx[i] + current.0
let nextY: Int = dy[i] + current.1
guard 0..<row ~= nextX && 0..<col ~= nextY else { continue }
if map[nextX][nextY] == 0 {
map[nextX][nextY] = 2
queue.append((nextX, nextY))
}
}
}
var count: Int = 0
for i in 0..<row {
for j in 0..<col {
if map[i][j] == 0 {
count += 1
}
}
}
answer = max(count, answer)
}