-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14503_로봇 청소기.swift
75 lines (63 loc) · 1.45 KB
/
14503_로봇 청소기.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
import Foundation
let nm = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
let rcd = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
// 서 남 동 북
let dr: [Int] = [0, 1, 0, -1]
let dc: [Int] = [-1, 0, 1, 0]
let n = nm[0]
let m = nm[1]
var r = rcd[0]
var c = rcd[1]
var d = 3-rcd[2]
var roomMap: [[Int]] = []
for _ in 0..<n {
let input = readLine()!
.components(separatedBy: " ")
.map { Int($0)! }
roomMap.append(input)
}
var currentRoom: Int = roomMap[r][c]
var answer: Int = 0
func checkNeedClean() -> Bool {
for i in dr.indices {
let nr = r + dr[i]
let nc = c + dc[i]
guard 0..<n ~= nr && 0..<m ~= nc else { continue }
if roomMap[nr][nc] == 0 {
return true
}
}
return false
}
while 0..<n ~= r
&& 0..<m ~= c
&& roomMap[r][c] != 1 {
currentRoom = roomMap[r][c]
if currentRoom == 0 {
roomMap[r][c] = -1
answer += 1
}
if checkNeedClean() == false {
r = r+dr[(d + 2) % dr.count]
c = c+dc[(d + 2) % dr.count]
} else {
var nr = r
var nc = c
while roomMap[nr][nc] != 0 {
d = (d + 1) % dr.count
nr = r + dr[d]
nc = c + dc[d]
if !(0..<n ~= nr) || !(0..<m ~= nc) {
nr = r
nc = c
}
}
r += dr[d]
c += dc[d]
}
}
print(answer)