#abc308d. D - Snuke Maze
D - Snuke Maze
Score : points
问题描述
我们有一个包含 行(水平)和 列(垂直)的网格。我们用 表示从上到下的第 行以及从左到右的第 列的单元格。网格中每个单元格上都写有一个小写英文字母。在 上书写的字母等于给定字符串 的第 个字符。
Snuke 将重复移动到相邻的共享边界的单元格,以从 移动到 。确定是否存在一条路径,在访问顺序上,所经过的单元格(包括初始点 和终点 )上的字母依次为 s
n
u
k
e
s
n
。这里,若满足 ,则称单元格 是与 共享边界的相邻单元格。
形式化地,确定是否存在一个单元格序列 满足以下条件:
- , ;
- 对于所有 , 是 的相邻且共享边界的单元格;以及
- 对于所有 ,在 上书写的字母与
snuke
的第 个字符相同。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
We have a grid with horizontal rows and vertical columns. We denote by the cell at the -th row from the top and -th column from the left. Each cell in the grid has a lowercase English letter written on it. The letter written on equals the -th character of a given string .
Snuke will repeat moving to an adjacent cell sharing a side to travel from to . Determine if there is a path in which the letters written on the visited cells (including initial and final ) are s
n
u
k
e
s
n
, in the order of visiting. Here, a cell is said to be an adjacent cell of sharing a side if and only if .
Formally, determine if there is a sequence of cells such that:
- ;
- is an adjacent cell of sharing a side, for all ; and
- the letter written on coincides with the -th character of
snuke
, for all .
Constraints
- and are integers.
- is a string of length consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
Output
Print Yes
if there is a path satisfying the conditions in the problem statement; print No
otherwise.
Sample Input 1
2 3
sns
euk
Sample Output 1
Yes
The path $(1,1) \rightarrow (1,2) \rightarrow (2,2) \rightarrow (2,3)$ satisfies the conditions because they have s
n
u
k
written on them, in the order of visiting.
Sample Input 2
2 2
ab
cd
Sample Output 2
No
Sample Input 3
5 7
skunsek
nukesnu
ukeseku
nsnnesn
uekukku
Sample Output 3
Yes
update @ 2024/3/10 08:44:23