Under construction

Still being built. Things may change

Pingvin

Interview

Time limit1sMemory limit1024 MB

Summary
Find the fewest unit moves along the x, y, or z axis needed to fly from the start cell to the end cell through cloud-free cells in an n by n by n cube.
Level

Easy3 of 10

Topics
BFS, Graph, Array
Solved
No attempts yet

Pingvin

Zrakoplović the penguin wants to learn how to fly.

The space in which he will learn to fly is a cube of dimensions n×n×nn \times n \times n, divided into n3n^3 unit cubes. Each unit cube is described by three coordinates (x,y,z)(x, y, z), where xx, yy and zz are integers between 11 and nn. The coordinate xx is the distance from the left edge of the space, the coordinate yy is the distance from the front edge of the space, and the coordinate zz is the height.

Some of these unit cubes contain clouds, and some do not.

Zrakoplović is afraid of clouds, so he will fly only where there are no clouds. He starts at position (xs,ys,zs)(x_s, y_s, z_s), where zs=1z_s = 1 (that is, at height 11), and wants to reach position (xe,ye,ze)(x_e, y_e, z_e).

He is practicing flight in directions parallel to one of the axes (the xx-axis, yy-axis or zz-axis). In one wing flap he can cross at most one unit cube.

Before he flies, Zrakoplović wants to know how many wing flaps he needs to reach the destination. Help him find that number.

The start and end positions are never clouds.

Examples3

  1. Example 1

    Input
    2
    1 1 1
    1 1 2
    00
    10
    01
    00
    
    Expected output
    1
    
  2. Example 2

    Input
    3
    2 3 1
    1 1 1
    000
    010
    000
    111
    111
    111
    111
    111
    111
    
    Expected output
    3
    
  3. Example 3

    Input
    3
    2 1 1
    3 2 2
    000
    010
    110
    010
    001
    001
    101
    110
    000
    
    Expected output
    3