#C4887. Can Reach Target
Can Reach Target
Can Reach Target
You are given a grid of size (n \times m) where each cell is either passable (0) or impassable (1). You are also provided with the starting cell ((sr, sc)) and the target cell ((tr, tc)) (both 1-indexed). Your task is to determine if there exists a path from the starting cell to the target cell by moving in one of the four cardinal directions (up, down, left, right) without leaving the grid boundaries. Note that if either the starting or the target cell is blocked, the answer is "NO".
The problem can be modeled as a grid traversal problem using Breadth-First Search (BFS).
inputFormat
The input is read from standard input (stdin) and follows this format:
- The first line contains two integers (n) and (m), representing the number of rows and columns in the grid, respectively.
- The next (n) lines each contain (m) integers (either 0 or 1) separated by spaces, describing the grid.
- The following line contains two integers (sr) and (sc), the row and column of the starting cell (1-indexed).
- The last line contains two integers (tr) and (tc), the row and column of the target cell (1-indexed).
outputFormat
Output to standard output (stdout) a single line containing either "YES" if there is a valid path from the starting cell to the target cell, or "NO" otherwise.## sample
5 7
0 0 0 1 0 0 0
0 1 0 1 0 1 0
0 1 0 0 0 1 0
0 0 1 1 0 1 0
1 0 0 0 0 0 0
1 1
5 7
YES