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 number a 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 and m (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.

题意:给了一个迷宫,迷宫中只有路的地方可以走,在迷宫的四周也都布满了一样的迷宫, 问可不可以一直无限的走下去

思路:在走迷宫的时候用两个坐标来标记,一个是实际的坐标(可能会有负的,可能会有超过n和m的),还有一个是对应到题目给的迷宫上的坐标,设走的点的实际的坐标是nx和ny,那么这个坐标对应在题目给的迷宫里面的坐标的就是(nx%n+n)%n和(ny%m+m)%m,在搜索的时候通过记录的实际的坐标点的结构体来判断这个点的对应在题目给的图里面有没有对应的,如果有的话就说明这个点已经走过了,如果在走过的情况下并且已经走过两次了,那么就说明这个是可以无限的走的,没有的话那么就要把这个实际的坐标点放进去,用来判断接下去会不会走过这些点

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include <set>
#include<queue>
#include<map>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn = ;
const ll mod = 1e9+;
const double eps = 1e-; struct node
{
int x,y;
bool operator == (const node &a) const {
return x==a.x && y==a.y;
}
};
int n,m;
char a[maxn][maxn];
node vis[maxn][maxn];
int v[maxn][maxn];
int dx[] = {,-,,};
int dy[] = {,,,-};
bool bfs(int sx,int sy)
{
memset(v,,sizeof v);
queue<node>que;
v[sx][sy] = true;
vis[sx][sy] = node{sx,sy};
que.push(node{sx,sy});
while(que.size())
{
node p = que.front();
que.pop();
for(int i=;i<;i++)
{
int nx = p.x + dx[i];
int ny = p.y + dy[i];
int rx = (nx%n+n)%n;
int ry = (ny%m+m)%m;
if(a[rx][ry] == '#')
continue;
node next = node {nx,ny};
if(v[rx][ry])
{
if(!(next == vis[rx][ry]))
return true;
}
else
{
v[rx][ry] = true;
vis[rx][ry] = next;
que.push(node{nx,ny});
}
}
}
return false;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<n;i++)
cin>>a[i];
int sx,sy;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j] == 'S')
{
sx = i;
sy = j;
}
}
}
if(bfs(sx,sy))
puts("Yes");
else
puts("No");
return ;
}

Infinite Maze CodeForces - 196B的更多相关文章

  1. CodeForces 196B Infinite Maze

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

  2. [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 ...

  3. Codeforces 197D - Infinite Maze

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

  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. Infinite Maze

    从起点开始走,对于可以走到的位置,都必定能从这个位置回到起点.这样,对地图进行搜索,当地图中的某一个被访问了两次,就能说明这个地图可以从起点走到无穷远. 搜索的坐标(x,y),x的绝对值可能大于n,的 ...

  6. 【codeforces 196B】Infinite Maze

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

  7. V - Infinite Prefixes CodeForces - 1295B math

    天哪!!菜到家啦. 数学+思维. 首先求出一个周期内cnt0-cnt1=c的个数,如果C=0,那么只要在一个周期内有前缀等于x,那么答案就是-1,否则答案就是0 如果C!=0,列一下方程x=t*c+a ...

  8. Codeforces Round #124 (Div. 2)

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

  9. HZNU 2019 Summer training 6 -CodeForces - 622

    A - Infinite Sequence  CodeForces - 622A 题目大意:给你一个这样的数列1,1,2,1,2,3,1,2,3,4,1,2,3,4,5....就是从1~n排列(n++ ...

随机推荐

  1. Java IO 整理总结

    read(byte b[], int off, int len) 方法的作用是从输入流中读取 len 个字节,并把数据写入到字节数组b中,并返回实际读取了多少数据.如果没有读取到任何数据,意味着文件已 ...

  2. React学习笔记 - JSX简介

    React Learn Note 2 React学习笔记(二) 标签(空格分隔): React JavaScript 一.JSX简介 像const element = <h1>Hello ...

  3. python入门10 循环语句

    两种循环: 1 for in 2 while #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 循环语句 ...

  4. UESTC-1259 昊昊爱运动 II

    昊昊爱运动 II Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)     昊昊喜 ...

  5. POJ 1416 Shredding Company【dfs入门】

    题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  6. [19/03/13-星期三] 数组_二维数组&冒泡排序&二分查找

    一.二维数组 多维数组可以看成以数组为元素的数组.可以有二维.三维.甚至更多维数组,但是实际开发中用的非常少.最多到二维数组(我们一般使用容器代替,二维数组用的都很少). [代码示例] import ...

  7. <知识整理>2019清北学堂提高储备D3

    全天动态规划入门到入坑... 一.总概: 动态规划是指解最优化问题的一类算法,考察方式灵活,也常是NOIP难题级别.先明确动态规划里的一些概念: 状态:可看做用动态规划求解问题时操作的对象. 边界条件 ...

  8. eclipce导出项目发布到tomcat

    1.右击项目-Except 2.在弹出框中输入“WAR file” 3.点击“next” 在Destinatin选择保存路径,即可 4.将保存的文件复制到tomcat下,启动tomcat之后,会自动解 ...

  9. MVC学习七:Razor布局之加载分部视图【PartialView】

    Partial View 顾名思义就是Html代码片段,应用于此HTML代码多次被页面加载时使用.(类似于WebForm程序中的用户控件) 注:PartialView和正常的View页面在访问时没有任 ...

  10. MySQL单列索引和联合索引

    MySQL单列索引和联合索引   所有的MySQL列类型能被索引.在相关的列上的使用索引是改进SELECT操作性能的最好方法. 一个表最多可有16个索引.最大索引长度是256个字节,尽管这可以在编译M ...