#K69672. Delivery Robot: Minimum Moves in a Grid City
Delivery Robot: Minimum Moves in a Grid City
Delivery Robot: Minimum Moves in a Grid City
You are given a grid representing a city with m rows and n columns. Each cell in the grid is either 0
(free) or 1
(obstacle). A delivery robot starts at the top-left corner (0,0) and must reach the bottom-right corner $(m-1, n-1)$.
The robot can move up, down, left, or right by one cell in each move, but it cannot move into cells with obstacles. Your task is to determine the minimum number of moves required for the robot to reach its destination. If there is no valid path, output -1
.
Note: The coordinates are defined in a 0-indexed manner.
inputFormat
The first line contains two integers m
and n
representing the number of rows and columns of the grid, respectively.
The following m
lines each contain n
space-separated integers (either 0
or 1
), representing the grid.
outputFormat
Output a single integer: the minimum number of moves required for the robot to travel from cell (0, 0)
to cell (m-1, n-1)
. If no valid path exists, output -1
.
3 3
0 0 0
0 1 0
0 0 0
4
</p>