#DSU0000055. Đường đi khép kín (Round Trip)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đường đi khép kín (Round Trip)
Round Trip
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a grid with rows and columns. Each cell is empty . or blocked #, except for exactly one cell marked S, the starting position. You may move to any of the four edge-adjacent cells and may not enter blocked cells.
Determine whether a walk can start at S, make some moves, and return to S without visiting any other cell more than once. Remaining in place does not count as a move; the route must form a genuine cycle of at least steps.
The final visit to S is permitted even though S was visited at the beginning. Output No if no such route exists.
Input
The first line contains with , followed by strings of length .
Output
Print Yes or No.
Subtasks
- Subtask 1 (20 points): , ; The grid contains only
.,#,Sand exactly one cellS. - Subtask 2 (30 points): , ; The grid contains only
.,#,Sand exactly one cellS. - Subtask 3 (50 points): , ; The grid contains only
.,#,Sand exactly one cellS.
Examples
Input
4 4
....
#.#.
.S..
.##.
Output
Yes
Explanation
One valid route is $(3,2)\to(3,3)\to(3,4)\to(2,4)\to(1,4)\to(1,3)\to(1,2)\to(2,2)\to(3,2)$, using 1-based coordinates. It returns to S without repeating an intermediate square, so the answer is Yes.