#C10163. Taco Pathfinding

    ID: 39338 Type: Default 1000ms 256MiB

Taco Pathfinding

Taco Pathfinding

You are given a grid representing a city map. Each cell in the grid is either an open space denoted by . or a building denoted by B. Your mission is to determine if there exists a path from the starting cell to the target cell, moving only in the four cardinal directions (up, down, left, right) and avoiding buildings.

The grid consists of n rows and m columns. The starting and target positions are provided using 0-indexed coordinates. If a valid path exists, print YES; otherwise, print NO.

Mathematically, let the grid be represented as a matrix \(G\) of size \(n \times m\), where \(G[i,j] = '.'\) indicates an open cell and \(G[i,j] = 'B'\) indicates a blocked cell. Determine whether there exists a sequence of moves from \((r_{start}, c_{start})\) to \((r_{end}, c_{end})\) such that each adjacent move is one cell up, down, left, or right and every visited cell is open.

inputFormat

The input is read from standard input (stdin) in the following format:

  1. The first line contains two integers n and m — the number of rows and columns of the grid.
  2. The next n lines each contain a string of length m representing the grid row. Each character is either . (an open space) or B (a building).
  3. The following line contains two integers, r_start and c_start, indicating the starting cell coordinates (0-indexed).
  4. The final line contains two integers, r_end and c_end, indicating the target cell coordinates (0-indexed).

outputFormat

Output to standard output (stdout) a single line containing either YES or NO. Print YES if there exists a path from the starting cell to the target cell; otherwise, print NO.

## sample
4 5
..B..
.B.B.
...B.
B....
0 0
2 4
YES