#K69782. Robot Navigation on a Grid
Robot Navigation on a Grid
Robot Navigation on a Grid
You are given an n x n grid. Each cell in the grid is either open ('.') or blocked ('#'). A robot starts at the top-left cell (0, 0) and needs to reach the bottom-right cell (n-1, n-1). The robot can move one cell at a time in the four cardinal directions (up, down, left, right) but can only move onto open cells. Your task is to determine if there exists a path from the start to the destination.
Note: The start or the destination might be blocked. If either of them is blocked, the answer is immediately NO.
The problem can be mathematically described as follows: Find if there exists a sequence of moves such that starting from (0,0) and applying moves in the set \(\{(\pm1,0), (0,\pm1)\}\), you eventually reach \((n-1, n-1)\), while remaining in the bounds \(0 \leq x, y < n\) and only stepping on cells where the grid value is .
.
inputFormat
The input is given via standard input with the following format:
n row1 row2 ... rown
Where:
n
is an integer representing the size of the grid.- Each
row
is a string of lengthn
consisting of the characters.
(open) and#
(blocked).
outputFormat
Output a single line: YES
if there exists a path from the top-left to the bottom-right cell; otherwise, output NO
.
4
....
.##.
.#..
....
YES