#K77207. Shortest Path in a 5x5 Grid
Shortest Path in a 5x5 Grid
Shortest Path in a 5x5 Grid
You are given a 5x5 grid represented by integers. Each cell in the grid can either be open (represented by 0) or blocked (represented by 1). Your task is to determine the length of the shortest path from the top-left cell (row 0, column 0) to the bottom-right cell (row 4, column 4), moving only up, down, left, or right. If no such path exists, return -1.
The grid is input via standard input (stdin) as 5 lines, each containing 5 space-separated integers. The output should be a single integer printed to standard output (stdout) which is the length of the shortest path.
The shortest path is defined as the minimum number of moves required to go from the start to the destination. When computing the path length, the starting cell is not counted as a move. For example, if you move right, then down, your path length is 2.
Formally, given a grid \(G\) of size \(5 \times 5\), find the minimum value of \(d\) such that there exists a sequence of moves from \(G[0][0]\) to \(G[4][4]\) and every move goes to an adjacent cell (up, down, left, right) that is open, i.e., has a value of 0. If there is no such sequence, return \(-1\).
inputFormat
The input consists of 5 lines. Each line contains 5 space-separated integers where each integer is either 0 (open cell) or 1 (blocked cell).
0 0 1 0 0 0 1 0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0
outputFormat
Output a single integer which is the length of the shortest path from the top-left corner to the bottom-right corner, or -1 if no such path exists.
## sample0 0 1 0 0
0 1 0 1 0
0 0 0 0 1
1 0 1 0 0
0 0 0 1 0
8