-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2638번.swift
81 lines (76 loc) · 1.97 KB
/
2638번.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
80
81
// 출처 : 백준 치즈
// https://www.acmicpc.net/problem/2638
// 풀이 : hogumachu
let size = readLine()!.split(separator: " ").map{Int(String($0))!}
var cheeze: [[Int]] = Array(repeating: [], count: size[0])
var result = 0
var visited = Array(repeating: Array(repeating: false, count: size[1]), count: size[0])
let saveVisited = visited
(0..<size[0]).forEach({
cheeze[$0] = readLine()!.split(separator: " ").map{Int(String($0))!}
})
while true {
visited = saveVisited
spread(0, 0)
for i in 0..<size[0] {
for j in 0..<size[1] {
if cheeze[i][j] == 1 {
visit(i, j)
}
}
}
var change = false
for i in 0..<size[0] {
for j in 0..<size[1] {
if cheeze[i][j] == -1 {
cheeze[i][j] = 0
change = true
}
}
}
if change {
result += 1
} else {
break
}
}
print(result)
func spread(_ x: Int, _ y: Int) {
if visited[x][y] {
return
}
visited[x][y] = true
if x + 1 < size[0] && !visited[x + 1][y] && cheeze[x + 1][y] == 0 {
spread(x + 1, y)
}
if x - 1 >= 0 && !visited[x - 1][y] && cheeze[x - 1][y] == 0 {
spread(x - 1, y)
}
if y + 1 < size[1] && !visited[x][y + 1] && cheeze[x][y + 1] == 0 {
spread(x, y + 1)
}
if y - 1 >= 0 && !visited[x][y - 1] && cheeze[x][y - 1] == 0 {
spread(x, y - 1)
}
}
func visit(_ x: Int, _ y: Int) {
if cheeze[x][y] != 1 {
return
}
var airCounting = 0
if x + 1 < size[0] && cheeze[x + 1][y] == 0 && visited[x + 1][y] {
airCounting += 1
}
if x - 1 >= 0 && cheeze[x - 1][y] == 0 && visited[x - 1][y] {
airCounting += 1
}
if y + 1 < size[1] && cheeze[x][y + 1] == 0 && visited[x][y + 1] {
airCounting += 1
}
if y - 1 >= 0 && cheeze[x][y - 1] == 0 && visited[x][y - 1] {
airCounting += 1
}
if airCounting >= 2 {
cheeze[x][y] = -1
}
}