#K72082. Path Existence Verification in a Grid City Map

    ID: 33674 Type: Default 1000ms 256MiB

Path Existence Verification in a Grid City Map

Path Existence Verification in a Grid City Map

Given an n x n grid representing a city map, each cell contains one of the characters: R, B, or P. A cell with 'B' is blocked, while cells with 'R' or 'P' are passable. Starting from the top-left corner (coordinate \(0,0\)), you can only move either to the right or down. Your task is to determine whether a path exists to reach the bottom-right corner (coordinate \(n-1,n-1\)) using only these allowed moves.

The allowed moves are defined as follows:

  • Right move: \((0,1)\)
  • Down move: \((1,0)\)

If a valid path exists, output "YES"; otherwise, output "NO".

inputFormat

The input is given from stdin in the following format:

n
row1
row2
...
rown

Where the first line contains an integer n representing the dimensions of the grid. Each of the next n lines contains n space-separated characters (each being 'R', 'B', or 'P').

For example:

4
R R R P
B B R R
R B R P
R R R R

Constraints:

  • \(1 \leq n \leq 1000\)

outputFormat

Output a single line to stdout containing either "YES" or "NO", indicating whether there exists a valid path from the top-left corner to the bottom-right corner under the given movement rules.

## sample
4
R R R P
B B R R
R B R P
R R R R
YES