아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Pingvin

시간 제한1초메모리 제한1024 MB

요약
장애물이 있는 3차원 격자에서 시작 칸에서 끝 칸까지 축 방향으로 한 칸씩만 움직일 때 필요한 최소 걸음 수를 구한다.
난이도

보통10점 중 4점

유형
그래프, BFS, 행렬
정답자
아직 제출이 없습니다

문제

Zrakoplović the penguin wants to learn how to fly!

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

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

Zrakoplović is afraid of clouds, so he will learn to fly only where there are no clouds. He initially finds himself at a position (x_s,y_s,z_s)(x\_s, y\_s, z\_s), such that z_s=1z\_s = 1 (i.e. at height 11), and wants to get to position (x_e,y_e,z_e)(x\_e, y\_e, z\_e).

At the moment, he is perfecting the skill of flying in directions that are parallel to one of the axes of space (i.e. in the direction of the xx-axis, yy-axis or zz-axis), and in one wing flap he can cross at most one unit cube.

Before he decides to fly, Zrakoplović wants to know how many wing flaps he needs to get to the desired position. While he is preparing for the flight, help him answer that question.

입력

The first line contains an integer nn (1≤n≤1001 ≤ n ≤ 100), the dimension of the space in which Zrakoplović learns to fly.

The second line contains three integers x_sx\_s, y_sy\_s and z_sz\_s (1≤x_s,y_s≤n1 ≤ x\_s, y\_s ≤ n, z_s=1z\_s = 1), the start position of Zrakoplović.

The third line contains three integers x_ex\_e, y_ey\_e and z_ez\_e (1≤x_e,y_e,z_e≤n1 ≤ x\_e, y\_e, z\_e ≤ n), the end position of Zrakoplović.

This is followed by nn binary matrices of dimensions n×nn \times n that describe the space, where the ii-th matrix describes the space at height ii. The upper-left corner has the coordinates (1,1,i)(1, 1, i). The row and column of the matrix correspond to the xx and yy coordinates, respectively.

'0' denotes a unit cube in which there are no clouds, and '1' denotes a unit cube in which there are clouds.

The start and end position of Zrakoplović will not be a cloud.

출력

In the first and only line, print the smallest number of wing flaps that Zrakoplović must make to reach the desired position. If Zrakoplović cannot reach the desired position, print '-1'.

힌트

Clarification of the first example:

Zrakoplović can reach the desired position in one wing flap by moving in the direction of the zz-axis for one unit cube.

Clarification of the third example: Zrakoplović can reach the desired position in three wing flaps by first moving to position (2,1,2)(2, 1, 2), then to (2,2,2)(2, 2, 2) and finally to (3,2,2)(3, 2, 2).

예제3

  1. 예제 1

    입력
    2
    1 1 1
    1 1 2
    00
    10
    01
    00
    
    예상 출력
    1
    
  2. 예제 2

    입력
    3
    2 3 1
    1 1 1
    000
    010
    000
    111
    111
    111
    111
    111
    111
    
    예상 출력
    3
    
  3. 예제 3

    입력
    3
    2 1 1
    3 2 2
    000
    010
    110
    010
    001
    001
    101
    110
    000
    
    예상 출력
    3