#K68892. Grid Path with One Jump
Grid Path with One Jump
Grid Path with One Jump
Alice is trapped in a grid maze represented by n rows and m columns. Each cell in the grid is either open (denoted by '.') or blocked (denoted by '#'). Starting from the top-left cell (0, 0), Alice can only move either to the right or downward. In addition, she is granted a special ability: she can jump over a single blocked cell once during her journey. However, the jump moves her two cells in the chosen direction (i.e. she skips over one cell) provided the landing cell is open. The task is to determine whether Alice can reach the bottom-right cell (n-1, m-1) using these constraints.
Note: Both the start and the destination cells must be open. If either is blocked, the answer is immediately NO
.
The movement choices are limited to rightward and downward directions only.
The problem can be formally described using the following conditions:
- Start at cell (0, 0).
- Allowed directions: right (0, +1) and down (+1, 0).
- If the next cell in the chosen direction is blocked and the jump has not been used yet, Alice can jump over it (i.e. skip one cell in that same direction) provided the landing cell is open.
- The journey is successful if Alice reaches the cell (n-1, m-1).
Your task is to output YES
if there is a valid path for Alice, otherwise output NO
.
inputFormat
The input is given via standard input and has the following format:
n m row1 row2 ... rown
Here, the first line consists of two integers n and m separated by a space representing the number of rows and columns respectively. This is followed by n lines where each line is a string of length m consisting of the characters '.' (open) and '#' (blocked).
outputFormat
Output a single line to standard output: YES
if Alice can reach the bottom-right cell according to the rules, or NO
if she cannot.
4 4
....
.##.
..#.
....
YES