#P9909. Cube Maze

    ID: 23054 Type: Default 1000ms 256MiB

Cube Maze

Cube Maze

Given an \(n \times n \times n\) cube that is divided into \(n^3\) unit cubes, some of which are obstacles and cannot be passed through. You are also given the starting coordinate and the ending coordinate. In one step, you can move from a unit cube to one of its six adjacent cubes (sharing a common face) if that cube is not an obstacle. Your task is to determine the minimum number of steps required to move from the start to the end.

The movement is allowed along the \(x\), \(y\), and \(z\) directions. The coordinates are 0-indexed.

inputFormat

The input starts with a single integer \(n\) (the dimension of the cube). The next two lines contain three integers each representing the starting coordinates \((x_s, y_s, z_s)\) and the ending coordinates \((x_e, y_e, z_e)\) respectively. Following that, there are \(n\) blocks of input, each representing one level of the cube. Each level consists of \(n\) lines, and each line contains \(n\) integers separated by spaces. A 0 indicates a free cell, and a 1 indicates an obstacle.

For example, a 2×2×2 cube input would be formatted as follows:

2
0 0 0
1 1 1
0 0
0 0
0 0
0 0

outputFormat

Output a single integer representing the minimum number of steps required to reach the ending coordinate from the starting coordinate. If it is impossible to reach the destination, output -1.

sample

2
0 0 0
1 1 1
0 0
0 0
0 0
0 0
3