#C10537. Robot Navigation
Robot Navigation
Robot Navigation
You are given an $R \times C$ grid where each cell is either empty (denoted by .
) or contains an obstacle (denoted by #
). A robot is placed at a starting position and must reach a target position in the grid. The robot can move in four directions: up, down, left, or right. It cannot move into cells with obstacles or leave the grid.
Your task is to compute the minimum number of moves required to travel from the starting cell to the target cell. If the target is unreachable, print -1
.
The movements obey the following relation: if the robot moves from cell (i, j) to cell (i + dx, j + dy), then \( (dx, dy) \) must be one of \( \{(-1,0), (1,0), (0,-1), (0,1)\} \).
inputFormat
The input is read from standard input (stdin) and has the following format:
T R C grid_row_1 grid_row_2 ... grid_row_R S_row S_col T_row T_col ... (repeated T times)
Here, T
is the number of test cases. For each test case, the first line contains two integers R
and C
that denote the number of rows and columns of the grid. The next R
lines describe the grid. Each of these lines is a string of length C
consisting of characters .
and #
. The following line contains four integers: the starting row, starting column, target row, and target column. The grid indices are 0-based.
outputFormat
For each test case, output a single integer on a new line. This integer is the minimum number of moves to reach the target from the starting position. If the target cannot be reached, output -1
.
3
5 5
.....
.#.#.
..#..
.....
.....
0 0 4 4
2 2
.#
#.
0 0 1 1
1 1
.
0 0 0 0
8
-1
0
</p>