The Great Escape

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 2.5s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Valentine's Day 2015 Contest

Marceline's songs were too powerful! The trapped princesses are breaking free of their cages and seem to be under a demonic trance. They chase Marceline through the Ice Kingdom, intent on wreaking havoc. These fanboys and fangirls seem to have a Fatal attraction to Marceline, so she soars away like an Eagle in order to escape the Ice Kingdom.

Unfortunately, she first has to navigate through the Ice King's ice labyrinth (made of cement). The labyrinth is a series of rooms shaped like rectangular prisms with floating obstacles in them. The levitating obstacles include ice shards, debris and penguins. They are represented as cubes, identified by X, Y and Z, which specify the location of the edges of an obstacle. Each test case represents one such room.

The room has width W, length L, and height H. There are N (0 \le N \le 50\,000) obstacles in the room. The exit to any labyrinth room is a 3 \times 1 \times 3 door where the top-right "cube" of the door is when W, L, and H (therefore, Marceline can exit from X = W-2 \ldots W, Y = L, Z = H-2 \ldots H). The "cubes" in the "grid" are numbered from 1 \ldots W, L, H. That is, W_i, L_i, and H_i (integers) each represent an edge and the three together represent an imaginary cube, and so they specify a cube, not a point.

For each move, Marceline can move one square up, down, left, right, backward or forward. She can't move diagonally (diagonals are bad luck to vampires that are featured in programming problems). Output the number of moves Marceline needs to make in order to reach the exit. She can only exit if she is directly in one of the "door cubes". She enters a room at X = 1, Y = 1, Z = 1.

It is guaranteed that the distance she has to travel is not zero because no one in their right mind (this includes the Ice King, who might not be all there but is still sane) would build a room that is just a Cartesian plane. Marceline will always be able to exit a room (otherwise it wouldn't really be an exit, would it?).

Input Specification

The first line will contain 3 space-separated integers W, L, and H (3 \le W, L, H \le 100).
The second line will contain the single integer N.
The next N lines will consist of a single-cube-sized obstacle in the space-separated format X, Y, Z.

Output Specification

The least number of moves Marceline has to make in order to be right in front of the door so that she can escape.

Sample Input

4 4 4
2 2 2
3 2 2
2 3 2
2 2 3
3 3 2
3 2 3
2 3 3
3 3 3

Sample Output



There are no relevant obstacles, so Marceline travels three "cubes" forward, one "cube" to the right, and one "cube" up to reach the bottom-left "cube" of the nine "cubes" (arranged in the rectangular prism with dimensions 3 \times 1 \times 3) from which she can exit.


  • 2
    BMP  commented on March 18, 2015, 5:44 p.m.

    typical hob

  • 3
    Xyene  commented on Feb. 15, 2015, 3:15 p.m.

    The testdata has been updated, and submissions have been rejudged.

    • 4
      FatalEagle  commented on Feb. 15, 2015, 3:16 p.m.

      Note that the data did not satisfy the original constraint that N < 30000, so constraints are updated.

      • 3
        bobhob314  commented on Feb. 15, 2015, 3:37 p.m.

        Whoops :O

  • 2
    Phoenix1369  commented on Feb. 15, 2015, 10:36 a.m.

    Were there supposed to be relevant obstacles? The shortest path also works... for all the cases apparently.

    • 0
      bobhob314  commented on Feb. 15, 2015, 2:06 p.m.

      The cases were supposed to be updated... but they haven't been as of yet.

      • 0
        bobhob314  commented on Feb. 15, 2015, 3:02 p.m.

        Waitin' on tar.gz