atcoder#ABC276E. [ABC276E] Round Trip
[ABC276E] Round Trip
Score : points
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
.
4 4
....
#.#.
.S..
.##.
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.
2 2
S.
.#
No
5 7
.#...#.
..#.#..
...S...
..#.#..
.#...#.
No