#K43722. Robo's Grid Navigation

    ID: 27373 Type: Default 1000ms 256MiB

Robo's Grid Navigation

Robo's Grid Navigation

Robo is a small robot that needs to navigate through a factory floor represented as a grid. The grid has ( R ) rows and ( C ) columns. Each cell in the grid is either free (denoted by '0') or blocked by an obstacle (denoted by '1'). Robo can move one step at a time in one of the four cardinal directions: up, down, left, or right. The task is to compute the minimum number of moves needed for Robo to travel from a starting cell to a target cell. If there is no path, the answer is (-1).

Note: All coordinates are given in 0-indexed format.

inputFormat

The input consists of several test cases. For each test case, the first line contains two integers ( R ) and ( C ) (with ( R, C \ge 1 )). The next ( R ) lines each contain a string of length ( C ) with characters either '0' or '1', representing the grid. The following line contains four integers: ( r_S ), ( c_S ), ( r_T ), and ( c_T ), representing the starting and target cell coordinates respectively. The input terminates with a test case where ( R = 0 ) and ( C = 0 ), which should not be processed.

outputFormat

For each test case, output a single integer on a new line: the minimum number of moves needed for Robo to reach the target cell, or (-1) if it is not possible.## sample

5 5
00000
01110
01110
01110
00000
0 0 4 4
3 3
000
010
000
0 0 2 2
3 3
000
111
000
0 0 2 2
1 1
0
0 0 0 0
0 0
8

4 -1 0

</p>