#K59522. Path Existence in a Grid

    ID: 30883 Type: Default 1000ms 256MiB

Path Existence in a Grid

Path Existence in a Grid

Given a 2-dimensional grid with dimensions \(n \times m\), where each cell is either open (represented by 0) or blocked (represented by 1), determine if there is a path from a starting cell to an ending cell. You can only move in the four cardinal directions (up, down, left, right).

The grid is provided along with the starting and ending coordinates. A path is considered to exist if there is a sequence of moves from the starting cell to the ending cell that only passes through open cells.

Input Format:

  • The first line contains two integers \(n\) and \(m\) representing the number of rows and columns of the grid.
  • The next \(n\) lines each contain \(m\) integers (either 0 or 1) representing the grid where 0 indicates an open cell and 1 indicates a blocked cell.
  • The following line contains two integers representing the starting cell coordinates (row and column, 0-indexed).
  • The last line contains two integers representing the ending cell coordinates (row and column, 0-indexed).

Output Format:

  • Output True if a path exists from the start to the end, otherwise output False.

Note: Movement is allowed only in horizontal and vertical directions.

inputFormat

The input is read from standard input (stdin) and follows the format below:

  • Line 1: Two integers \(n\) and \(m\) representing the number of rows and columns.
  • Next \(n\) lines: Each line contains \(m\) space-separated integers representing the grid.
  • Next line: Two integers denoting the starting cell coordinates (row and column).
  • Last line: Two integers denoting the ending cell coordinates (row and column).

outputFormat

The output should be printed to standard output (stdout) as a single line which is either True or False depending on whether there exists a path from the starting cell to the ending cell.

## sample
4 4
0 1 0 0
0 0 1 0
1 0 1 0
0 0 0 0
0 0
3 3
True