#abc276e. E - Round Trip
E - Round Trip
Score : points
问题描述
我们有一个从上到下有 行、从左到右有 列的网格。令 表示从顶部数起第 行()和从左边数起第 列()的方格。
每个方格属于以下三种类型之一:初始点、道路或障碍物。
若方格 对应的字符为 ,则当 S
表示该方格是初始点,当 .
表示是道路,而当 #
表示是障碍物。存在恰好一个初始点。
判断是否存在一条长度为 或更长的路径,它从初始点出发,重复垂直或水平移动到相邻方格,并在不经过障碍物且除起点和终点外不重复访问同一方格的情况下返回初始点。 更正式地,确定是否存在一个整数 和一系列方格坐标 满足以下条件:
- 。
-
S
。 - 如果 ,则
.
。 - 如果 ,则 。
- 如果 ,则方格 和方格 是垂直或水平相邻的。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
We have a grid with rows from top to bottom and columns from left to right. Let denote the -th row from the top and -th column from the left .
Each square is one of the following: the initial point, a road, and an obstacle.
A square is represented by a character . The square is the initial point if S
, a road if .
, and an obstacle if #
. There is exactly one initial point.
Determine whether there is a path of length or greater that starts at the initial point, repeats moving vertically or horizontally to an adjacent square, and returns to the initial point without going through an obstacle or visiting the same square multiple times except at the beginning and the end.
More formally, determine whether there are an integer and a sequence of squares that satisfy the following conditions.
- .
-
S
. - If , then
.
. - If , then .
- If , then square and square are vertically or horizontally adjacent to each other.
Constraints
- and are integers greater than or equal to .
- is
S
,.
, or#
. - There is exactly one such that
S
.
Input
The input is given from Standard Input in the following format:
Output
If there is a path that satisfies the conditions in the Problem Statement, print Yes
; otherwise, print No
.
Sample Input 1
4 4
....
#.#.
.S..
.##.
Sample Output 1
Yes
The path $(3, 2) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (1, 4) \rightarrow (2, 4) \rightarrow (3, 4) \rightarrow (3, 3) \rightarrow (3, 2)$ satisfies the conditions.
Sample Input 2
2 2
S.
.#
Sample Output 2
No
Sample Input 3
5 7
.#...#.
..#.#..
...S...
..#.#..
.#...#.
Sample Output 3
No
update @ 2024/3/10 11:37:59