#K35547. Taco: Grid Path with One Obstacle Removal
Taco: Grid Path with One Obstacle Removal
Taco: Grid Path with One Obstacle Removal
You are given an m by n grid where each cell is either free (denoted by .
) or contains an obstacle (denoted by #
). Starting from the top-left corner, your goal is to determine whether it is possible to reach the bottom-right corner by moving only to the right or down. You are allowed to remove at most one obstacle from the grid.
A valid path is one that only moves in the right ((0, 1)) or down ((1, 0)) directions and traverses cells containing .
(free cells). Formally, if we denote the cell coordinates by \((i,j)\) with \(0 \leq i \lt m\) and \(0 \leq j \lt n\), you need to check whether there exists a path from \((0,0)\) to \((m-1,n-1)\) after optionally converting one \(#
\) into a free cell.
The problem can be stated in mathematical terms as follows:
Determine if there exists a sequence of moves \(\{(i_k, j_k)\}\) such that
[ \begin{aligned} &(i_0, j_0) = (0,0),,\ &(i_{K}, j_{K}) = (m-1, n-1),,\ &(i_{k+1} - i_k, j_{k+1} - j_k) \in {(0,1),(1,0)},,\ &\text{and for each } (i,j) \text{ in the path, the cell is free (possibly after one removal)}. \end{aligned} ]
inputFormat
The input is provided via standard input (stdin) and consists of the following lines:
- The first line contains two integers
m
andn
--- the number of rows and columns respectively. - The following
m
lines each contain a string of lengthn
composed of characters.
and#
, representing the grid.
You can assume that 1 ≤ m, n ≤ 100
.
outputFormat
Output a single line via standard output (stdout) containing either Yes
if it is possible to reach the bottom-right cell under the given conditions or No
otherwise.
3 3
..#
.#.
...
Yes