#K68877. Reachability in a Grid Maze

    ID: 32962 Type: Default 1000ms 256MiB

Reachability in a Grid Maze

Reachability in a Grid Maze

You are given a grid of size (H \times W) containing obstacles and open cells. Each cell in the grid is either open (denoted by a dot '.') or blocked (denoted by a hash '#' ). The grid is indexed from (0) to (H-1) in rows and (0) to (W-1) in columns. You start at position ((R, C)) and your goal is to reach the bottom-right cell ((H-1, W-1)). You can move in four directions: up, down, left, and right. Note that if the destination cell ((H-1, W-1)) is blocked or if there is no valid path avoiding obstacles, you should output "No"; otherwise, output "Yes".

Movement and Constraints:

  • You can move from cell \((i, j)\) to cell \((i + dx, j + dy)\) where \((dx, dy)\) can be \((-1,0)\), \((1,0)\), \((0,-1)\), or \((0,1)\).
  • The indices satisfy \(0 \leq i < H\) and \(0 \leq j < W\).

Use a breadth-first search (BFS) or any equivalent method to determine reachability through valid moves.

inputFormat

The input is given through standard input (stdin) and consists of the following:

  1. A single line containing four space-separated integers: (H), (W), (R), and (C), representing the number of rows, number of columns, starting row index, and starting column index respectively.
  2. (H) lines follow, each containing a string of length (W) composed of the characters '.' (open cell) and '#' (blocked cell), representing the grid.

Note: (R) and (C) are zero-indexed.

outputFormat

Print a single line to standard output (stdout) that is either "Yes" if there is a valid path from the start cell ((R, C)) to the bottom-right cell ((H-1, W-1)) avoiding obstacles, or "No" otherwise.## sample

5 5 0 0
.....
.###.
.#...
.#.##
.....
Yes