D - Infinite Maze

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).

Example

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.

题目的大意就是,给一张网格图,某些地方可以走,其余的则不行,然后某个人从某个点出发,一直在迷宫走,如果走出边界,则回到这个迷宫内相应的地方(当然要可以走),问你是否能走到一个"新的"起点位置.

一开始,我以为这题很水,DFS一遍就好,在四个边界上,上下,左右的同一个位置,找一下是否都能从起点访问到,就输出yes.后面发现这个想法太naive了,好的反例能hack掉,又加了一道,但是又被hack...

然后失去了信心.到比赛结束后才发现反例,然后很难改,于是换了一种思路,直接根据题意进行模拟就好了,知道满足要求,然后竟然就过了...qwq

 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #define mp make_pair
 using namespace std;
 ,fl[][]={{,},{,},{-,},{,-}};
 int n,m,Sx,Sy;
 pair<int,int> vis[maxn][maxn];
 char c[maxn][maxn];
 bool v[maxn][maxn];
 &&x<n&&y>-&&y<m&&c[x][y]!='#'&&!v[x][y];}
 bool DFS(int x,int y){
     int xx=x,yy=y;
     ) xx+=n; xx%=n;
     ) yy+=m; yy%=m;
     );}
     ;
     v[xx][yy]=,vis[xx][yy]=mp(x,y);
     ; i<; i++) DFS(x+fl[i][],y+fl[i][]);
 }
 int main(){
     scanf(];
     ; i<n; i++){
         scanf(; j<m; j++){
             c[i][j]=s[j];
             if (c[i][j]=='S') Sx=i,Sy=j;
         }
     }
     if (DFS(Sx,Sy)) puts("Yes"); else puts("No");
     ;
 }

[CodeForces - 197D] D - Infinite Maze的更多相关文章

  1. 【codeforces 196B】Infinite Maze

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

  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. CodeForces 196B Infinite Maze

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

  5. xtu summer individual 3 C.Infinite Maze

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

  6. Infinite Maze

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

  7. CodeForces 622 A.Infinite Sequence

    A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. codeforces 675A A. Infinite Sequence(水题)

    题目链接: A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input st ...

  9. codeforces 622A A. Infinite Sequence (二分)

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. Git回顾

    抄自廖雪峰的官方网站 完整图文请访问https://github.com/Mrlution/study/tree/master/git 关于repository 我认为repository是一个存放代 ...

  2. Leetcode121-Best Time to Buy and Sell Stock I - Easy

    I Say you have an array for which the ith element is the price of a given stock on day i. If you wer ...

  3. Spring3.x 版本和 JDK1.8 不兼容导致 java.lang.IllegalStateException: Failed to load ApplicationContext

    由于安装了 JDK1.8 的版本,最近在进行整合 Struts2+Spring+Hibernate 框架的时候,不小心导入了之前下载的 Spring 3.2.0 版本的 jar 包. 结果在运行测试用 ...

  4. JaveWeb 公司项目(6)----- 通过ToolTip给控件添加动态注释

    现在公司的项目进展到了视屏这一块,关于海康网页端的构建我会另外写一篇博客来详细讲解,这一篇的博文主要讲的是我刚刚遇到的一个小问题 连接上了视屏之后,将控制按钮换成图标,方位按钮比较好理解,调焦调距的按 ...

  5. FreeCodeCamp---advanced Algorithm Scripting解法

    Exact Change 设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) ...

  6. crontab 定时执行python脚本

    每天8点30分运行命令/tmp/run.sh * * * /tmp/run.sh 每两小时运行命令/tmp/run.sh */ * * * /tmp/run.sh

  7. 深入JVM对象引用

    在jdk 1.2以前,创建的对象只有处在可触及(reachaable)状态下,才能被程序所以使用,垃圾回收器一旦发现无用对象,便会对其进行回收.但是,在某些情况下,我们希望有些对象不需要立刻回收或者说 ...

  8. Activity回传值报错:Failure delivering result ResultInfo{who=null,request=7,result = 0,data=null}

    Activity  A   -----值------->  Activity  B   -----值----->  Activity  A     场景:当A跳转到B,再从B直接点击返回按 ...

  9. nRF52832-PPI部分学习

    PPI部分学习思维导图 PPI原理 1.1PPI简介 PPI实现的就是通过初始化配置,将不同外设的事件和任务连接起来,让事件自动去触发任务的功能,PPI有多个通道, 每个通道包含一个EEP和TEP,使 ...

  10. springboot 解决配置js/css/img缓存问题

    # 解决配置js/css/img缓存问题 spring.resources.chain.strategy.content.enabled=true spring.resources.chain.str ...