-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17144번.swift
104 lines (89 loc) · 2.52 KB
/
17144번.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// 출처 : 백준 미세먼지 안녕!
// https://www.acmicpc.net/problem/17144
// 풀이 : hogumachu
import Foundation
func solution() {
let input = readLine()!.split(separator: " ").map{Int(String($0))!}
let r = input[0], c = input[1], t = input[2]
var table: [[Int]] = Array(repeating: [], count: r)
var saveTable: [[Int]] = Array(repeating: Array(repeating: 0, count: c), count: r)
var k = 0
var countT = 0
var result = 0
for i in 0..<r {
table[i] = readLine()!.split(separator: " ").map{Int(String($0))!}
if table[i][0] == -1 && k == 0 {
k = i
}
}
func spread(_ x: Int, _ y: Int) -> Void {
let divValue = table[x][y] / 5
var count = 0
if x-1 >= 0 {
if table[x-1][y] != -1 {
saveTable[x-1][y] += divValue
count += 1
}
}
if x+1 < r{
if table[x+1][y] != -1 {
saveTable[x+1][y] += divValue
count += 1
}
}
if y-1 >= 0 {
if table[x][y-1] != -1 {
saveTable[x][y-1] += divValue
count += 1
}
}
if y+1 < c {
if table[x][y+1] != -1 {
saveTable[x][y+1] += divValue
count += 1
}
}
saveTable[x][y] += table[x][y] - divValue * count
}
func airCleaner(_ k: Int) -> Void {
for i in 0..<k-1 {
table[k-1-i][0] = table[k-2-i][0]
}
table[0][0...c-2] = table[0][1...c-1]
for i in 0..<k {
table[i][c-1] = table[i+1][c-1]
}
table[k][2...c-1] = table[k][1...c-2]
table[k][1] = 0
for i in k+2...r-1 {
table[i-1][0] = table[i][0]
}
table[r-1][0...c-2] = table[r-1][1...c-1]
for i in k+1...r-2 {
table[r-1+k+1-i][c-1] = table[r-1+k+1-i-1][c-1]
}
table[k+1][2...c-1] = table[k+1][1...c-2]
table[k+1][1] = 0
table[k][0] = -1
table[k+1][0] = -1
}
while t > countT {
countT += 1
for i in 0..<r {
for j in 0..<c {
if table[i][j] != 0 && table[i][j] != -1 {
spread(i, j)
}
}
}
table = saveTable
airCleaner(k)
saveTable = Array(repeating: Array(repeating: 0, count: c), count: r)
}
for i in table {
result += i.reduce(0, +)
}
result += 2
print(result)
}
solution()