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. HDU 5727 Necklace(全排列+二分图匹配)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 题意:现在有n个阳珠子和n个阴珠子,现在要把它们串成项链,要求是阴阳珠子间隔串,但是有些阴阳珠 ...

  2. PHP 冒泡排序(Bubble Sort)

    冒泡排序指的是依次比较相邻的两个数,然后根据大小做出排序,直至最后两位数.因为在排序的 过程中总是小数放前面,大数放后面,和气泡上升有点类似,所以又称作冒泡排序. 下面通过一个实例看一下如何实现冒泡排 ...

  3. git中 .ignore文件的配置 忽略不想上传的文件

    1.配置语法: 以斜杠“/”开头表示目录: 以星号“*”通配多个字符: 以问号“?”通配单个字符 以方括号“[]”包含单个字符的匹配列表: 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录: 此外,g ...

  4. pom中配置的仓库无效的问题

    今天在用spring cloud的时候发现,配置的pom仓库一直无效(官网要求2.0版本直接从指定仓库里下).于是上网搜索,发现(http://18810098265.iteye.com/blog/2 ...

  5. Hexo 的next主题下添加网易云音乐作BGM

    首先,你要看看你选中的歌能不能在网页版的网易云音乐生成外链,因为版权保护原因,有些音乐是生不成外链的,比如这样的: 所以,选些可以生成外链的音乐.生成对应的外链 比如这里的重点是HTML代码中的src ...

  6. 解决在Vue项目中时常因为代码缩进导致页面报错的问题

    前言 如果我们初次使用vue-cli来构建单页SPA应用,在撸代码的过程中有可能会遇到这种因为代码缩进导致 页面报错的问题,导致我们烦不胜烦.接下来我们就来看一看如何解决这个小问题... erro原因 ...

  7. 用sql plus时,显示协议适配器错误

    1.在桌面右击我的电脑图标——选择栏中选择管理,点击并进入计算机管理 2.进入计算机管理界面后,点击服务和应用程序,然后在右边栏目选择服务,双击进入服务进程 3.进入服务进程后,鼠标下滑,一直下滑找到 ...

  8. MYSQL常用函数(格式化函数)

    DATE_FORMAT(date,fmt)  依照字符串fmt格式化日期date值 FORMAT(x,y)   把x格式化为以逗号隔开的数字序列,y是结果的小数位数 INET_ATON(ip)   返 ...

  9. 网络cmd命令

    1.ping ip; 检测IP地址是否相通 ping命令的常用参数选项 ping IP -t:连续对IP地址执行ping命令,直到被用户以Ctrl+C中断. ping IP -l 2000:指定pin ...

  10. c# 静态构造函数与私有构造函数共存

    在使用静态构造函数的时候应该注意几点: 1.静态构造函数既没有访问修饰符,也没有参数.因为是.NET调用的,所以像public和private等修饰符就没有意义了. 2.是在创建第一个类实例或任何静态 ...