#C4686. Taco Island Shortest Path

    ID: 48251 Type: Default 1000ms 256MiB

Taco Island Shortest Path

Taco Island Shortest Path

You are given a grid representing Taco Island. Each cell in the grid is either land (denoted by 0) or water (denoted by 1). The grid has dimensions \( n \times m \), where \( n \) is the number of rows and \( m \) is the number of columns. The coordinates are provided in a 1-indexed format.

Your task is to determine the length of the shortest path from a given starting cell to a destination cell. Movement is allowed only in the four cardinal directions: up, down, left, and right. The path must traverse only through land cells. If either the starting or destination cell is water or if there is no valid path between them, your program should output \(-1\). Note that if the starting cell is the same as the destination cell, the shortest path length is \(0\).

inputFormat

The input is read from standard input (stdin) in the following format:

  • The first line contains two space-separated integers ( n ) and ( m ), which denote the number of rows and columns of the grid, respectively.
  • Each of the next ( n ) lines contains ( m ) space-separated integers (each either 0 or 1) representing the grid.
  • The next line contains two space-separated integers ( r_1 ) and ( c_1 ), representing the 1-indexed coordinates of the starting cell.
  • The final line contains two space-separated integers ( r_2 ) and ( c_2 ), representing the 1-indexed coordinates of the destination cell.

outputFormat

Output a single integer to standard output (stdout): the length of the shortest path from the starting cell to the destination cell. If no such path exists, output (-1).## sample

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