#P1363. 幻象迷宫

幻象迷宫

Description

The illusion maze can be regarded as infinite, but it is formed by repeating several N×MN \times M matrices. In the matrix, some cells are roads, denoted by \verb!.!, and some cells are walls, denoted by \verb!#!. The position of LHX and WD is denoted by \verb!S!. That is, for a point (x,y)(x,y) in the maze, if (xmodn,ymodm)(x \bmod n,y \bmod m) is \verb!.! or \verb!S!, then this location is a road; if (xmodn,ymodm)(x \bmod n,y \bmod m) is \verb!#!, then this location is a wall. LHX and WD can move in the four directions up, down, left, and right, and of course they cannot move into walls. Here, nn and mm refer to NN and MM respectively.

Please tell LHX and WD whether they can escape from the illusion maze (if they can reach positions at unbounded distance from the starting point, we consider that they can escape). Otherwise, LHX will have to activate the castle’s self-destruction program... but he really does not want to do that unless absolutely necessary.

Input Format

The input contains multiple test cases.

For each test case, the first line contains two integers N,MN, M.

Then follows an N×MN \times M character matrix, representing the cells from (0,0)(0,0) to (n1,m1)(n-1,m-1).

Output Format

For each test case, output a string, "Yes" or "No".

5 4
##.#
##S#
#..#
#.##
#..#
5 4
##.#
##S#
#..#
..#.
#.##

Yes
No


Hint

  • For 30% of the testdata, 1N,M201 \le N, M \le 20.
  • For 50% of the testdata, 1N,M1001 \le N, M \le 100.
  • For 100% of the testdata, 1N,M15001 \le N, M \le 1500, with no more than 1010 test cases per test point.

Translated by ChatGPT 5