#C452. Maze Shortest Path

    ID: 48067 Type: Default 1000ms 256MiB

Maze Shortest Path

Maze Shortest Path

In this problem, you are given a maze represented as a grid of size n×mn \times m, where each cell is either passable (denoted by 0) or blocked by a wall (denoted by 1). Your task is to determine the length of the shortest path from a specified starting cell to a target cell. Movement is allowed in four directions: up, down, left, and right. If the target cell is unreachable, output -1. A Breadth-First Search (BFS) approach can be used to solve this problem within a time complexity of O(n×m)O(n \times m).

inputFormat

The first line contains two space-separated integers n and m, indicating the number of rows and columns of the maze respectively.

The following n lines each contain m space-separated integers (0 or 1) representing the maze grid, where '0' denotes a passable cell and '1' denotes a wall.

The next line contains two space-separated integers: start_x and start_y (0-indexed), representing the starting cell.

The final line contains two space-separated integers: target_x and target_y (0-indexed), representing the target cell.

outputFormat

Output a single integer representing the length of the shortest path from the starting cell to the target cell. If there is no valid path, output -1.

## sample
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 1 0
0 1 0 1 0
0 0 0 0 0
0 0
4 4
8