-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15683_감시.swift
214 lines (175 loc) · 5.21 KB
/
15683_감시.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import Foundation
let input = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
let n = input[0]
let m = input[1]
var roomTable: [[Int]] = []
for _ in 0..<n {
let input = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
roomTable.append(input)
}
typealias CCTVInfo = (type: Int, x: Int, y: Int)
var cctvInfos: [CCTVInfo] = []
var countOfZero: Int = 0
var answer: Int = Int.max
// 상 좌 하 우
let dx: [Int] = [-1, 0, 1, 0]
let dy: [Int] = [0, -1, 0, 1]
for x in 0..<n {
for y in 0..<m {
let room = roomTable[x][y]
if 1...5 ~= room {
cctvInfos.append((room, x, y))
} else if room == 0 {
countOfZero += 1
}
}
}
func checkSpot(_ x: Int, _ y: Int, _ direction: Int, _ roomTable: inout [[Int]]) -> Int {
var nx = x + dx[direction]
var ny = y + dy[direction]
var countOfCheck: Int = 0
while 0..<n ~= nx && 0..<m ~= ny && roomTable[nx][ny] != 6 {
if roomTable[nx][ny] == 0 {
countOfCheck += 1
roomTable[nx][ny] = -1
}
nx += dx[direction]
ny += dy[direction]
}
return countOfCheck
}
func runCCTV(_ cctvInfo: CCTVInfo, _ direction: Int, _ roomTable: inout [[Int]]) -> Int {
var countOfCheck: Int = 0
switch cctvInfo.type {
case 1:
countOfCheck += checkSpot(cctvInfo.x, cctvInfo.y, direction, &roomTable)
case 2:
[0, 2].forEach {
countOfCheck += checkSpot(cctvInfo.x, cctvInfo.y, (direction+$0)%4, &roomTable)
}
case 3:
(0...1).forEach {
countOfCheck += checkSpot(cctvInfo.x, cctvInfo.y, (direction+$0)%4, &roomTable)
}
case 4:
(0...2).forEach {
countOfCheck += checkSpot(cctvInfo.x, cctvInfo.y, (direction+$0)%4, &roomTable)
}
case 5:
(0...3).forEach {
countOfCheck += checkSpot(cctvInfo.x, cctvInfo.y, (direction+$0)%4, &roomTable)
}
default:
break
}
return countOfCheck
}
func bt(_ indexOfcctvInfo: Int, _ countOfCheck: Int, _ roomTable: [[Int]]) {
if indexOfcctvInfo == cctvInfos.count {
let countOfBlindSpot = countOfZero - countOfCheck
answer = min(countOfBlindSpot, answer)
return
}
var nowTable = roomTable
for i in dx.indices {
let newCountOfCheck = runCCTV(cctvInfos[indexOfcctvInfo], i, &nowTable)
bt(indexOfcctvInfo+1, countOfCheck+newCountOfCheck, nowTable)
nowTable = roomTable
}
}
bt(0, 0, roomTable)
print(answer)
/*--------------------------------------------------------------------------------------------------*/
/*
import Foundation
let input = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
let n = input[0]
let m = input[1]
var roomTable: [[Int]] = []
var cctvInfos: [(type: Int, x: Int, y: Int)] = []
var zeroCount: Int = 0
// 상, 우, 하, 좌
let dx: [Int] = [-1, 0, 1, 0]
let dy: [Int] = [0, 1, 0, -1]
var answer: Int = Int.max
for _ in 0..<n {
let input = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
roomTable.append(input)
}
for x in 0..<n {
for y in 0..<m {
if 1...5 ~= roomTable[x][y] {
cctvInfos.append((roomTable[x][y], x, y))
} else if roomTable[x][y] == 0 {
zeroCount += 1
}
}
}
func coverZeroOneLine(_ tempRoomTable: inout [[Int]], _ x: Int, _ y: Int, _ dir: Int) -> Int {
var nx = x + dx[dir]
var ny = y + dy[dir]
var coverCount = 0
while 0..<n ~= nx && 0..<m ~= ny && tempRoomTable[nx][ny] != 6 {
if tempRoomTable[nx][ny] == 0 {
tempRoomTable[nx][ny] = -1
coverCount += 1
}
nx += dx[dir]
ny += dy[dir]
}
return coverCount
}
func runCCTV(_ cctvType: Int, _ x: Int, _ y: Int, _ dir: Int, _ tempRoomTable: inout [[Int]]) -> Int {
var coverCount = 0
switch cctvType {
case 1:
coverCount += coverZeroOneLine(&tempRoomTable, x, y, dir)
case 2:
[0, 2].forEach {
coverCount += coverZeroOneLine(&tempRoomTable, x, y, (dir + $0) % 4)
}
case 3:
(0...1).forEach {
coverCount += coverZeroOneLine(&tempRoomTable, x, y, (dir + $0) % 4)
}
case 4:
(0...2).forEach {
coverCount += coverZeroOneLine(&tempRoomTable, x, y, (dir + $0) % 4)
}
case 5:
(0...3).forEach {
coverCount += coverZeroOneLine(&tempRoomTable, x, y, (dir + $0) % 4)
}
default:
break
}
return coverCount
}
func dfs(_ index: Int, _ coverCount: Int, _ tempRoomTable: [[Int]]) {
if index >= cctvInfos.count {
let blindSpotCount = zeroCount - coverCount
answer = min(answer, blindSpotCount)
return
}
var nowTable = tempRoomTable
let nowCCTV = cctvInfos[index]
for dir in dx.indices {
let nowCoverCount = runCCTV(nowCCTV.type, nowCCTV.x, nowCCTV.y, dir, &nowTable)
dfs(index + 1, coverCount + nowCoverCount, nowTable)
nowTable = tempRoomTable
}
}
dfs(0, 0, roomTable)
print(answer)
*/
/*
- 참고: https://0urtrees.tistory.com/227
*/