#K32872. Taco: Path Existence in Grid
Taco: Path Existence in Grid
Taco: Path Existence in Grid
You are given a grid consisting of m rows and n columns. Each cell in the grid contains either a 0 or a 1. A cell with a value of 1 is passable, and a cell with a value of 0 is blocked. Given the starting cell at coordinates \((s_x, s_y)\) and the target cell at coordinates \((t_x, t_y)\), your task is to determine whether there exists a path from the starting cell to the target cell moving only in the four cardinal directions (up, down, left, right).
You can assume that the grid indices are 0-indexed and that you cannot move out of bounds. If a cell has already been visited, it cannot be revisited in the same path.
Input/Output Specification: The input is read from standard input and the output is written to standard output.
In mathematical form, there exists a path if and only if there is a sequence of cells \((x_0,y_0), (x_1,y_1), \dots, (x_k,y_k)\) such that:
[ \begin{aligned} (x_0, y_0) &= (s_x, s_y),\ (x_k, y_k) &= (t_x, t_y),\ |x_{i+1} - x_i| + |y_{i+1} - y_i| &= 1 \quad &\text{for all } i,\ \text{and } & grid[x_i][y_i] = 1 \quad &\text{for all } i. \end{aligned} ]
inputFormat
The input is given as follows:
- The first line contains two integers m and n, representing the number of rows and columns, respectively.
- The next m lines each contain n integers (0 or 1) separated by spaces, representing the grid.
- The last line contains four integers: sx, sy, tx, and ty, which denote the starting cell and the target cell coordinates.
outputFormat
Output a single line containing either True
if there exists a path from the starting cell to the target cell; otherwise, output False
.
4 4
1 0 1 1
1 1 0 1
0 1 1 0
1 1 1 1
0 0 3 3
True
</p>