#abc308d. D - Snuke Maze

D - Snuke Maze

Score : 400400 points

问题描述

我们有一个包含 HH 行(水平)和 WW 列(垂直)的网格。我们用 (i,j)(i,j) 表示从上到下的第 ii 行以及从左到右的第 jj 列的单元格。网格中每个单元格上都写有一个小写英文字母。在 (i,j)(i,j) 上书写的字母等于给定字符串 SiS_i 的第 jj 个字符。

Snuke 将重复移动到相邻的共享边界的单元格,以从 (1,1)(1,1) 移动到 (H,W)(H,W)。确定是否存在一条路径,在访问顺序上,所经过的单元格(包括初始点 (1,1)(1,1) 和终点 (H,W)(H,W))上的字母依次为 s \rightarrow n \rightarrow u \rightarrow k \rightarrow e \rightarrow s \rightarrow n \rightarrow \dots。这里,若满足 i1i2+j1j2=1|i_1-i_2|+|j_1-j_2| = 1,则称单元格 (i1,j1)(i_1,j_1) 是与 (i2,j2)(i_2,j_2) 共享边界的相邻单元格。

形式化地,确定是否存在一个单元格序列 ((i1,j1),(i2,j2),,(ik,jk))((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k)) 满足以下条件:

  • (i1,j1)=(1,1)(i_1,j_1) = (1,1), (ik,jk)=(H,W)(i_k,j_k) = (H,W)
  • 对于所有 t (1t<k)t\ (1 \leq t < k)(it+1,jt+1)(i_{t+1},j_{t+1})(it,jt)(i_t,j_t) 的相邻且共享边界的单元格;以及
  • 对于所有 t (1tk)t\ (1 \leq t \leq k),在 (it,jt)(i_t,j_t) 上书写的字母与 snuke 的第 (((t1)mod5)+1)(((t-1) \bmod 5) + 1) 个字符相同。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a grid with HH horizontal rows and WW vertical columns. We denote by (i,j)(i,j) the cell at the ii-th row from the top and jj-th column from the left. Each cell in the grid has a lowercase English letter written on it. The letter written on (i,j)(i,j) equals the jj-th character of a given string SiS_i.

Snuke will repeat moving to an adjacent cell sharing a side to travel from (1,1)(1,1) to (H,W)(H,W). Determine if there is a path in which the letters written on the visited cells (including initial (1,1)(1,1) and final (H,W)(H,W)) are s \rightarrow n \rightarrow u \rightarrow k \rightarrow e \rightarrow s \rightarrow n \rightarrow \dots, in the order of visiting. Here, a cell (i1,j1)(i_1,j_1) is said to be an adjacent cell of (i2,j2)(i_2,j_2) sharing a side if and only if i1i2+j1j2=1|i_1-i_2|+|j_1-j_2| = 1.

Formally, determine if there is a sequence of cells ((i1,j1),(i2,j2),,(ik,jk))((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k)) such that:

  • (i1,j1)=(1,1),(ik,jk)=(H,W)(i_1,j_1) = (1,1),(i_k,j_k) = (H,W);
  • (it+1,jt+1)(i_{t+1},j_{t+1}) is an adjacent cell of (it,jt)(i_t,j_t) sharing a side, for all t (1t<k)t\ (1 \leq t < k); and
  • the letter written on (it,jt)(i_t,j_t) coincides with the (((t1)mod5)+1)(((t-1) \bmod 5) + 1)-th character of snuke, for all t (1tk)t\ (1 \leq t \leq k).

Constraints

  • 2H,W5002\leq H,W \leq 500
  • HH and WW are integers.
  • SiS_i is a string of length WW consisting of lowercase English letters.

Input

The input is given from Standard Input in the following format:

HH WW

S1S_1

S2S_2

\vdots

SHS_H

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 \rightarrow n \rightarrow u \rightarrow 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