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

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

Infinite Maze的更多相关文章

  1. [CodeForces - 197D] D - Infinite Maze

    D - Infinite Maze We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wal ...

  2. Codeforces 197D - Infinite Maze

    197D - Infinite Maze 思路:bfs,如果一个点被搜到第二次,那么就是符合要求的. 用vis[i][j].x,vis[i][j].y表示i,j(i,j是取模过后的值)这个点第一次被搜 ...

  3. Infinite Maze CodeForces - 196B

    We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A ...

  4. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  5. CodeForces 196B Infinite Maze

    Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. 【codeforces 196B】Infinite Maze

    [题目链接]:http://codeforces.com/problemset/problem/196/B [题意] 给你一个n*m的棋盘; 然后你能够无限复制这个棋盘; 在这个棋盘上你有一个起点s; ...

  7. Codeforces Round #124 (Div. 2)

    A. Plate Game 如果可以放置一个圆的情况下,先手将圆放置在矩形正中心,那么根据对称性,先手只要放后手的对称的位置即可,也就是先手必胜,否则后手胜. B. Limit 讨论\(n,m\)的大 ...

  8. Meandering Through the Maze of MFC Message and Command Routing MFC消息路由机制分析

    Meandering Through the Maze of MFC Message and Command Routing Paul DiLascia Paul DiLascia is a free ...

  9. Backtracking algorithm: rat in maze

    Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...

随机推荐

  1. 使用sublime text3搭建Python编辑环境

    最近在工作遇到一个难题. 我所在的测试组有一套PC软件前端自动化工程,在进行自动化测试时,需要在一台古老的xp机器上运行,但这台古老的xp机器带给我诸多烦恼,特别是使用Pycharm编辑器时,我遇到了 ...

  2. Flutter 布局类组件:流式布局(Wrap和Flow)

    前言 把超出屏幕显示范围会自动折行的布局称为流式布局.Flutter中通过Wrap和Flow来支持流式布局,将Row换成Wrap后溢出部分则会自动折行. Wrap 接口描述 Wrap({ Key ke ...

  3. 【.NET 与树莓派】使用 GPIO 库

    上回老周在说准备工作的时候,提到过树莓派用金属盒散热的事情.有朋友会说,加了金属盒子接线不方便,就算用了"T"形板,毕竟是把导线延长了的.其实扩展板就是把原有的引脚引出(类似于延长 ...

  4. 【Azure Developer】Python代码通过AAD认证访问微软Azure密钥保管库(Azure Key Vault)中机密信息(Secret)

    关键字说明 什么是 Azure Active Directory?Azure Active Directory(Azure AD, AAD) 是 Microsoft 的基于云的标识和访问管理服务,可帮 ...

  5. 聊聊 g0

    很多时候,当我们跟着源码去理解某种事物时,基本上可以认为是以时间顺序展开,这是编年体的逻辑.还有另一种逻辑,纪传体,它以人物为中心编排史事,使得读者更聚焦于某个人物.以一种新的视角,把所有的事情串连起 ...

  6. 超过varchar定义长度

    mysql> select version();+------------+| version() |+------------+| 5.1.73-log |+------------+1 ro ...

  7. 【UML】Use Case Diagrams

    文章目录 Use Case Diagrams Introduction Use case Diagram Use Case Diagrams - Actors Use Case Diagrams – ...

  8. 【Oracle】从删除的recyclebin中查看并恢复数据

    如果数据库中用了drop删除表,后面没有加上purge的话,会出现在oracle的回收机制中 dba_recyclebin可以查看当前删除的都是哪些 这个只是部分截图,可以看到删除的对象是什么,删除的 ...

  9. ctfhub技能树—文件上传—00截断

    什么是00截断 相关教程:http://www.admintony.com/%E5%85%B3%E4%BA%8E%E4%B8%8A%E4%BC%A0%E4%B8%AD%E7%9A%8400%E6%88 ...

  10. vue路由切换和用location切换url的区别

    最近的业务涉及到了axios的拦截器,要在request.js里面要根据状态码来跳转页面,这时候我就面对了几种跳转选择: 1.使用location.href='/url'来跳转,简单方便,但是刷新了页 ...