-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path072.swift
More file actions
60 lines (50 loc) · 1.24 KB
/
072.swift
File metadata and controls
60 lines (50 loc) · 1.24 KB
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
func readInts() -> [Int] {
readLine()!.split(separator: " ").map { Int(String($0))! }
}
let hw = readInts()
let h = hw[0]
let w = hw[1]
var geo: [[Character]] = []
for _ in 1...h {
geo.append(Array(readLine()!))
}
var passed = [[Bool]](repeating: [Bool](repeating: false, count: w), count: h)
var originX = 0
var originY = 0
var counter = -1
for y in 0..<h {
for x in 0..<w {
if geo[y][x] == "#" {
continue
}
originX = x
originY = y
seeFrom(x: x, y: y, times: 0)
}
}
func seeFrom(x: Int, y: Int, times: Int) {
for (dX, dY) in [(1, 0), (-1, 0), (0, 1), (0, -1)] {
let nextX = x + dX
let nextY = y + dY
if nextX >= 0 && nextX <= w - 1
&& nextY >= 0 && nextY <= h - 1
&& geo[nextY][nextX] == "."
&& passed[nextY][nextX] == false {
walkTo(x: nextX, y: nextY, times: times + 1)
}
}
}
func walkTo(x: Int, y: Int, times: Int) {
if x == originX && y == originY {
if times >= 3 {
if times > counter {
counter = times
}
}
return
}
passed[y][x] = true
seeFrom(x: x, y: y, times: times)
passed[y][x] = false
}
print(counter)