CodeForces196B. Infinite Maze(BFS)

版权声明:Amove? https://blog.csdn.net/Amovement/article/details/87644468

                                                B. 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的地图,实际上很多张一模一样的地图平铺在这张地图的周围。

可以理解为该人无法走回头路,询问这个人能否无限走下去。

三、大致思路

要想无穷走下去,只要这个人从( x , y )出发,再在别的地图上又达到了这个(x' , y' )点。

在BFS的基础上,用 rx、ry 表示这个人的真实坐标,xx、yy 则是这个人在原图上的对应的虚拟位置。

现在我们在经过一个点时给这个点打上标记,标记的值是到这个虚拟点时所在的真实坐标。

这样每当我们经过一个点就检查一下当前的真实位置和当初记录的位置是否不同,如果不同,说明它是真正的第二次到达该点。

四、代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;
const int inf = 0x3f3f3f3f;
#define LL long long int
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }



int n,m;
int sx,sy;
char mmp[1505][1505];
struct Node
{
    int x,y;
    void init()
    {
        x=-inf;y=-inf;
    }
    Node(){}
    Node(int _x,int _y)
    {
        x=_x;y=_y;
    }
    void Set(int _x,int _y)
    {
        x=_x;y=_y;
    }
}vis[1505][1505];

int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

bool BFS()
{
    queue<Node>q;
    q.push(Node(sx,sy));
    vis[sx][sy].Set(sx,sy);
    while(!q.empty())
    {
        Node t=q.front();q.pop();
        for(int i=0;i<4;i++)
        {
            int rx=t.x+dx[i],ry=t.y+dy[i];
            int xx=(rx%n+n)%n,yy=(ry%m+m)%m;
            if(mmp[xx][yy]!='#')
            {
                if(vis[xx][yy].x!=-inf&&vis[xx][yy].y!=-inf)
                {
                    if(vis[xx][yy].x!=rx||vis[xx][yy].y!=ry)return true;
                }
                else
                {
                    vis[xx][yy].Set(rx,ry);
                    q.push(Node(rx,ry));
                }
            }

        }
    }
    return false;

}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%s",mmp[i]);
        for(int j=0;j<m;j++)
        {
            vis[i][j].init();
            if(mmp[i][j]=='S')
            {
                sx=i;sy=j;
            }
        }
    }
    cout<<(BFS()?"Yes":"No")<<endl;
}

猜你喜欢

转载自blog.csdn.net/Amovement/article/details/87644468