#K7996. Taco Chocolate Path
Taco Chocolate Path
Taco Chocolate Path
You are given a chocolate bar represented as an n × m grid. Each cell of the grid is either a sweet piece denoted by 'S' or a bitter piece denoted by 'B'.
Your task is to determine whether there exists a path from the top-left corner to the bottom-right corner of the grid that passes only through sweet pieces ('S'). Movement is allowed in the four cardinal directions: up, down, left, and right.
Formally, let \(G\) be an \(n \times m\) grid. You need to check if there exists a sequence of cells \((i_1,j_1), (i_2,j_2), \dots, (i_k,j_k)\) such that:
- \((i_1, j_1) = (0,0)\) and \((i_k, j_k) = (n-1, m-1)\),
- For each \(1 \leq t \leq k\), \(G[i_t][j_t] = 'S'\),
- For each consecutive pair \((i_t, j_t)\) and \((i_{t+1}, j_{t+1})\), the Manhattan distance is 1.
If such a path exists, output YES
; otherwise, output NO
.
inputFormat
The input is read from standard input (stdin) and is in the following format:
n m row_1 row_2 ... row_n
where:
n
is the number of rows.m
is the number of columns.- Each
row_i
is a string of lengthm
consisting only of characters 'S' and 'B'.
outputFormat
Output a single line to standard output (stdout) containing either YES
if there exists a valid path, or NO
otherwise.
3 3
SSS
SBS
SSS
YES