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

搜索的坐标(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. (解决)easypoi图片导出只占用一个单元格

    @ 目录 前提 依赖环境 问题原因 解决方案 重写jar中的方法 原理 前提 本解决方案来源于网络,因解决自己需求,因此自行记录起来,如有侵权请联系我. 依赖环境 easypoi--依赖版本3.1.0 ...

  2. Mac上最好用的软件集合,没有之一

    前言 题主从 windows 系统换成 macOS 系统已经4年多了.对于没有用过 Mac 电脑的人来说,可能无法理解 Mac 好用在哪里.不过对于一个用过 Mac 的开发者来说,从 windows ...

  3. 浅谈 Checkbox Group 的双向数据绑定

    前言 不曾想在忙碌的工作面前,写一篇技术博客也成了奢求. Checkbox 作为表单中最常见的一类元素,使用方式分为单值和多值,其中单值的绑定很简单,就是 true 和 false,但是多值(Chec ...

  4. SQL LEN()函数用法

    含义: LEN 函数返回文本字段中值的长度. 返回字符表达式中的字符数 SQL LEN() 语法 SELECT LEN(column_name) FROM table_name 举例: 1.LEN对相 ...

  5. 攻防世界—pwn—cgpwn2

    题目分析 题目提示 checksec检查文件保护机制 使用ida查看伪代码 hello函数存在溢出,与level2类似 信息收集 system地址 name的地址 编写脚本 from pwn impo ...

  6. 细数JS中实用且强大的操作符&运算符

    目录 1,前言 2,代码+应用 2.1,短路运算符 || 2.2,短路运算符 && 2.3,零合并操作符 ?? 2.4,可选链操作符 ?. 2.5,位运算符 & 和 | 2.6 ...

  7. JS实现鼠标移入DIV随机变换颜色

    今天分享一个在 JavaScript中,实现一个鼠标移入可以随机变换颜色,本质就是js的随机数运用. 代码如下: <!DOCTYPE html> <html> <head ...

  8. wordpress迁移报错

    背景: 因为一些原因迁移wordpress的博客.备份好数据库和网站源码到另一台生产环境上线的时候报错: Warning: require(/www/wwwroot/pazzn/wp-includes ...

  9. (06)-Python3之--判断、循环

    1.判断(if) 语法: if 条件(True/False): 条件为真时,执行的代码(要干的事情)[elif 条件: 条件为真时,执行的代码(要干的事情)elif 条件: 条件为真时,执行的代码(要 ...

  10. git的使用学习笔记---合并分支

    一.使用场景 不同的分支需要合并 二.操作 1.首先要创建一个分支 git checkout -b mergedemo 创建文本 vim text.txt 添加文本 git add text.txt ...