#C7819. Taco: Valid Alphabet Path

    ID: 51732 Type: Default 1000ms 256MiB

Taco: Valid Alphabet Path

Taco: Valid Alphabet Path

You are given an \(n \times m\) grid consisting of lowercase English alphabets. Your task is to determine whether there exists a valid path from the top-left corner to the bottom-right corner of the grid.

A path is valid if:

  • You can only move in the four cardinal directions (up, down, left, right).
  • Each step moves to a cell whose character is strictly greater (in lexicographical order) than the previous one. In other words, if you are at a cell with character \(c\), you may move to a neighboring cell with character \(d\) only if \(c < d\) (following \(a < b < \cdots < z\)).

Print YES if such a path exists, and NO otherwise.

inputFormat

The input is read from stdin and is structured as follows:

  • The first line contains two integers \(n\) and \(m\), representing the number of rows and columns respectively.
  • The next \(n\) lines each contain a string of length \(m\), representing a row of the grid.

outputFormat

Output a single line to stdout containing either YES if there exists a valid path from the top-left to the bottom-right corner under the given conditions, or NO otherwise.## sample

4 4
abcd
bdef
cfgh
dghi
YES