#C10163. Taco Pathfinding
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:
- The first line contains two integers
n
andm
— the number of rows and columns of the grid. - The next
n
lines each contain a string of lengthm
representing the grid row. Each character is either.
(an open space) orB
(a building). - The following line contains two integers,
r_start
andc_start
, indicating the starting cell coordinates (0-indexed). - The final line contains two integers,
r_end
andc_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
.
4 5
..B..
.B.B.
...B.
B....
0 0
2 4
YES