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

搜索的坐标(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 }

最新文章

  1. Curl http_code 状态码 意义及信息
  2. eclipse文本编码格式修改为UTF-8 (转)
  3. 详解JavaScript中的Url编码/解码,表单提交中网址编码
  4. 在Web应用和IntelliJ IDEA中使用Spring框架
  5. 02~ 一步一步教你使用 SVN之SVN 的介绍
  6. Python urllib2 模块学习笔记
  7. Visual Studio的.NET内存分配分析器解析
  8. SWT/RAP计算器
  9. Mock原理学习
  10. 【Android Developers Training】 102. 序言:让你的应用获知地点
  11. Python——Scrapy初学
  12. 【笔记】h5 页面唤起电话呼叫
  13. 【Objective-C学习笔记】变量和基本的数据类型
  14. YII 自封装的批量修改的mysql操作类
  15. LVS简介与使用
  16. sklearn.preprocessing.LabelBinarizer
  17. c#随机生成英文名
  18. 面向对象特征:封装、多态 以及 @propetry装饰器
  19. Urllib库及cookie的使用
  20. ES6基础三(对象)

热门文章

  1. 写一个react hook:useLoading
  2. go语言环境搭建以及配置VSCode
  3. 使用vs code搭建Q#开发环境 (Mac)
  4. .NET 5 程序高级调试-WinDbg
  5. Payment Spring Boot 1.0.4.RELEASE 发布,最易用的微信支付 V3 实现
  6. MongoDB导出导入功能
  7. 3610:20140827:161308.483 No active checks on server: host [192.168.1.10] not found
  8. AgileConfig - RESTful API 介绍
  9. MybatisPlus多数据源及事务解决思路
  10. Python批量 png转ico