CodeForces 196B Infinite Maze

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

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
Copy
5 4
##.#
##S#
#..#
#.##
#..#
output
Copy
Yes
input
Copy
5 4
##.#
##S#
#..#
..#.
#.##
output
Copy
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的迷宫,在一个平面上有无限个这种迷宫,每个迷宫的上下左右都是完全相同的这种n*m的迷宫(方向和里面的布局都完全一样),也就是说以起始点为原点,所有(x,y)与(x%n,y%m)的内容(不包括‘s’)是相同的(模了数,迷宫在平面上周期性排列,若x<0时要(x%n+n)%n,y<0时同理),比如在这个迷宫走到了边界位置,如果另一个迷宫对应位置没有墙,就可以从另一个迷宫下边界进入,问是否能一直走下去使得离出发点的欧拉距离无限远

思路:以原点为坐标,若能走到无限远处,则在途中必定会在不同的迷宫中经过相同的对应点(有点类似同余模,把当前坐标模了之后可以对应相同的坐标),所以问题就转换为是否能找到不同的坐标有相同的对应点,若有则可以走到无限远,否则就不行

再放个test25的样例,这个要输出Yes

12 12
##.#######.#
#..#......S#
#.#..#######
..#.###.....
##..##..####
#..##..#####
..##..#.....
###..##.####
##..#...####
#..##.######
..##..####..
##...#####.#

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int amn=2e3,inf=0x3f3f3f3f;
 4 int n,m;
 5 char mp[amn][amn];
 6 bool f;
 7 int dt[5][3]={{0,1},{0,-1},{1,0},{-1,0}},stx,sty;
 8 struct node{
 9     int x, y;
10 };
11 node used[amn][amn];
12 queue<node> q;
13 void bfs(int sx,int sy){
14     node a;
15     a.x=sx;
16     a.y=sy;
17     used[sx][sy].x=sx;
18     used[sx][sy].y=sy;
19     q.push(a);
20     int x,y,dx,dy;
21     while(q.size()){
22         a=q.front();q.pop();
23         for(int i=0;i<4;i++){
24             dx=a.x+dt[i][0];
25             dy=a.y+dt[i][1];
26             x=(dx%n+n)%n,y=(dy%m+m)%m;    ///把负数坐标变为正数 x=(x%n+n)%n;负数取模会输出负数这时加上模数就是正数的对应值如-6%3=0,(3-0)%3=0,-5%3=-2,(3-2)%3=1,-4%3=-1,(3-1)%3=2...
27             //cout<<x<<' '<<y<<endl;
28             if(mp[x][y]=='#')continue;
29                 if(used[x][y].x==inf){
30                 used[x][y].x=dx;
31                 used[x][y].y=dy;
32                 node po;
33                 po.x=dx;
34                 po.y=dy;
35                 q.push(po);
36                 }
37                 else if(used[x][y].x!=dx||used[x][y].y!=dy){    ///如果当前坐标模出来后能对应一个不同的坐标,则是可以走到无限远的
38                     f=1;
39                     return ;
40                 }
41         }
42     }
43 }
44 int main(){
45     cin>>n>>m;
46     for(int i=0;i<n;i++){
47         for(int j=0;j<m;j++){
48             cin>>mp[i][j];
49             if(mp[i][j]=='S'){stx=i,sty=j;}
50             used[i][j].x=inf;
51             used[i][j].y=inf;
52         }
53     }
54     f=0;
55     bfs(stx,sty);
56     if(f)printf("Yes\n");
57     else printf("No\n");
58 }
59 /***
60 给出一个n*m的迷宫,在一个平面上有无限个这种迷宫,每个迷宫的上下左右都是完全相同的这种n*m的迷宫(方向和里面的布局都完全一样),
61 也就是说以起始点为原点,所有(x,y)与(x%n,y%m)的内容(不包括‘s’)是相同的(模了数,迷宫在平面上周期性排列,若x<0时要(x%n+n)%n,y<0时同理),
62 比如在这个迷宫走到了边界位置,如果另一个迷宫对应位置没有墙,就可以从另一个迷宫下边界进入,问是否能一直走下去使得离出发点的欧拉距离无限远
63 
64 以原点为坐标,若能走到无限远处,则在途中必定会在不同的迷宫中经过相同的对应点(有点类似同余模,把当前坐标模了之后可以对应相同的坐标),所以问题就转换为
65 是否能找到不同的坐标有相同的对应点,若有则可以走到无限远,否则就不行
66 ***/

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
06-30 18:19
点赞 评论 收藏
分享
就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务