-
Notifications
You must be signed in to change notification settings - Fork 0
/
18a.scala
51 lines (43 loc) · 1.41 KB
/
18a.scala
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
// scala 18a.scala
import scala.io.Source
import scala.collection.mutable.ArrayBuffer
object Solution {
val N = 1000
val DX = Array[Int](1, 0, -1, 0)
val DY = Array[Int](0, 1, 0, -1)
def solve(lines: ArrayBuffer[String]) = {
val a = Array.fill(N) { Array.fill(N) { false } }
var cur = (N / 2, N / 2)
a(cur._1)(cur._2) = true
for (line <- lines) {
val Array(dirStr, lenStr, _) = line.split(" ")
val dir = "DRUL".indexOf(dirStr)
for (it <- 0 until lenStr.toInt) {
cur = (cur._1 + DX(dir), cur._2 + DY(dir))
a(cur._1)(cur._2) = true
}
}
val q = new ArrayBuffer[(Int, Int)]()
var qb = 0
q.append((N / 2 + 1, N / 2 + 1))
while (qb < q.size) {
val (x, y) = q(qb)
qb += 1
if (!a(x)(y)) {
a(x)(y) = true
for (d <- 0 until 4) {
val (xx, yy) = (x + DX(d), y + DY(d))
if ((0 until N contains xx) && (0 until N contains yy) && !a(xx)(yy)) {
q.append((xx, yy))
}
}
}
}
(0 until N).map { i =>
(0 until N).map { j => if (a(i)(j)) 1 else 0 }.sum
}.sum
}
def main(args: Array[String]) = {
println(solve(Source.stdin.getLines.to(ArrayBuffer)))
}
}