程帅霞

不断受挫,不停起身,不断追寻,不止AC~~

导航

Infinite Maze

从起点开始走,对于可以走到的位置,都必定能从这个位置回到起点。这样,对地图进行搜索,当地图中的某一个被访问了两次,就能说明这个地图可以从起点走到无穷远。

  搜索的坐标(x,y),x的绝对值可能大于n,的绝对值可能大于m,(x,y)对应在基础地图的位置为rx=(nx%n+n)%n,ry=(ny%m+m)%m

 

We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (x, y) is a wall if and only if cell  is a wall.

In this problem  is a remainder of dividing numbera by number b.

The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (x, y) he can go to one of the following cells: (x, y - 1), (x, y + 1), (x - 1, y) and (x + 1, y), provided that the cell he goes to is not a wall.

Input

The first line contains two space-separated integers n andm (1 ≤ n, m ≤ 1500) — the height and the width of the maze that the boy used to cyclically tile the plane.

Each of the next n lines contains m characters — the description of the labyrinth. Each character is either a "#", that marks a wall, a ".", that marks a passable cell, or an "S", that marks the little boy's starting point.

The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input.

Output

Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).

Examples

Input
5 4
##.#
##S#
#..#
#.##
#..#
Output
Yes
Input
5 4
##.#
##S#
#..#
..#.
#.##
Output
No

Note

In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up.

In the second sample the vertical path is blocked. The path to the left doesn't work, too — the next "copy" of the maze traps the boy.

Sponsor

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 #define N 2000
 7 #define inf 0x3f3f3f3f
 8 int a[N][N],b[N][N];
 9 int f[4][2]={1,0,0,1,0,-1,-1,0};
10 char s[N][N];
11 int n,m,x,y;
12 struct node{
13     int x,y;
14 }u,v;
15 bool dfs()
16 {
17     queue<node> q;
18     u.x=x;
19     u.y=y;
20     q.push(u);
21     while(!q.empty())
22     {
23         u=q.front(); q.pop();
24         for(int i=0;i<4;i++)
25         {
26             v.x=u.x+f[i][0];
27             v.y=u.y+f[i][1];
28             int tx=(v.x%n+n)%n;
29             int ty=(v.y%m+m)%m;
30             if(s[tx][ty]!='#')
31             {
32                 if(a[tx][ty]==inf)
33                 {
34                     a[tx][ty]=v.x;
35                     b[tx][ty]=v.y;
36                     q.push(v);
37                 }
38                 else{
39                     if(a[tx][ty]!=v.x||b[tx][ty]!=v.y) return 1;
40                 } 
41             }
42         }
43     }
44     return 0;
45 }
46 int main()
47 {
48     while(~scanf("%d%d",&n,&m)&&(n+m))
49     {
50         for(int i=0;i<n;i++)
51         {
52             scanf("%s",s[i]);
53             for(int j=0;j<m;j++)
54             {
55                 if(s[i][j]=='S')
56                 {
57                     x=i,y=j;
58                 }
59                 a[i][j]=inf;
60                 b[i][j]=inf;
61             }
62         }
63         if(dfs()) printf("Yes\n");
64         else printf("No\n");
65     }
66 }

 

posted on 2020-09-23 21:06  程帅霞  阅读(100)  评论(0编辑  收藏  举报