#K90722. Matrix Path Finder
Matrix Path Finder
Matrix Path Finder
You are given a 2D grid consisting of 0s and 1s. A 0 represents an open cell and a 1 represents a blocked cell. Your task is to determine whether there exists a path from the top-left cell (0-indexed) to the bottom-right cell. You can only move to adjacent cells in the four main directions (up, down, left, right), and you can only move on cells with a 0.
Note: The starting cell (top-left) and the destination cell (bottom-right) must be 0 to be considered a valid path.
The problem can be mathematically modeled as follows:
Given a matrix \( A \) of dimensions \( R \times C \), determine if there exists a sequence of valid moves such that:
\[ A_{0,0} = 0, \quad A_{R-1, C-1} = 0, \quad \text{and} \quad \forall (i,j) \text{ in the path, } A_{i,j} = 0. \]If such a path exists, output true
; otherwise, output false
.
inputFormat
The first line contains two integers, R and C (the number of rows and columns respectively). If R or C is 0, the grid is considered empty.
The next R lines each contain C space-separated integers (each integer is either 0 or 1) representing the grid.
outputFormat
Output a single line containing true
if there exists a path from the top-left to the bottom-right of the grid; otherwise, output false
.
3 3
0 0 1
1 0 0
1 0 0
true