-
Notifications
You must be signed in to change notification settings - Fork 0
/
10a.kt
75 lines (70 loc) · 1.84 KB
/
10a.kt
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 kotlin.io.path.Path
import kotlin.io.path.readLines
import kotlin.math.*
fun <T> debug(container: Iterable<T>) {
container.forEach { element -> print("$element ") }
println()
}
val west = "-LF"
val east = "-7J"
val north = "|F7"
val south = "|LJ"
fun plusStart(dir: String): String {
return dir + "S"
}
fun add(ele: Pair<Int, Int>, Q: MutableList<Pair<Int, Int>>, SQ: MutableSet<Pair<Int, Int>>) {
Q += ele
SQ += ele
}
fun main() {
val fileName = "10.in"
val tab = Path(fileName).readLines()
var start = 0 to 0
val n = tab.size
val m = tab[0].length
for (i in 0 until n) {
for (j in 0 until m) {
if (tab[i][j] == 'S') start = i to j
}
}
var Q = mutableListOf(start)
var SQ = mutableSetOf(start)
var step = 0
while (step < Q.size) {
val (cx, cy) = Q[step]
for ((dx, dy) in arrayOf(0 to 1, 0 to -1, 1 to 0, -1 to 0)) {
val nx = cx + dx
val ny = cy + dy
if (0 <= nx && nx <= n && 0 <= ny && ny < m) {
if (!SQ.contains(nx to ny)) {
if (dx to dy == 0 to 1 &&
plusStart(west).contains(tab[cx][cy]) &&
east.contains(tab[nx][ny])
) {
add(nx to ny, Q, SQ)
}
if (dx to dy == 0 to -1 &&
plusStart(east).contains(tab[cx][cy]) &&
west.contains(tab[nx][ny])
) {
add(nx to ny, Q, SQ)
}
if (dx to dy == 1 to 0 &&
plusStart(north).contains(tab[cx][cy]) &&
south.contains(tab[nx][ny])
) {
add(nx to ny, Q, SQ)
}
if (dx to dy == -1 to 0 &&
plusStart(south).contains(tab[cx][cy]) &&
north.contains(tab[nx][ny])
) {
add(nx to ny, Q, SQ)
}
}
}
}
step++
}
println(Q.size / 2)
}