#C10111. Minimum Time to Reach Destination

    ID: 39281 Type: Default 1000ms 256MiB

Minimum Time to Reach Destination

Minimum Time to Reach Destination

You are given an ( n \times n ) grid representing a map. Each cell in the grid is either empty (denoted by 0) or an obstacle (denoted by 1). A robot starts at a given starting cell ((sx, sy)) and needs to reach the destination cell ((dx, dy)). In one move, the robot can travel to an adjacent cell in one of the four directions: up, down, left, or right, provided that the cell is not blocked by an obstacle. If the destination is unreachable, output (-1).

The task is to determine the minimum number of moves required for the robot to reach the destination. This problem can be solved efficiently using a breadth-first search (BFS) on the grid.

inputFormat

Input is given via (stdin) in the following format:

(n)
Next (n) lines, each containing (n) space-separated integers (either 0 or 1) representing the grid.
A line containing two integers: (sx) and (sy), representing the starting cell coordinates.
A line containing two integers: (dx) and (dy), representing the destination cell coordinates.

outputFormat

Output to (stdout) a single integer representing the minimum number of moves required for the robot to reach the destination. If the destination cannot be reached, print (-1).## sample

5
0 0 1 0 0
0 1 0 1 0
0 0 0 0 0
1 1 0 1 0
0 0 0 0 0
0 0
4 4
8