Codeforces Round #124 (Div. 2) 197D

Codeforces Round #124 (Div. 2)

Problem Description

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.


题意

图为无穷大,问能否从S点出发,走到距离S点无穷远处,即一直往远处走。

思路

从S点走到无穷远处,能走到另一组图的某个在原图中已被走过的点,若有这样的点,即可以走到距离S点无穷远出。BFS即可。

坑点

题意的理解


代码

#include <bits/stdc++.h>
 
using namespace std;
 
const int N=1505;
const int dir[4][2]= {-1,0,0,-1,1,0,0,1};
 
struct S
{
    int x;
    int y;
};
 
char state[N][N];
int vis[N][N];
int viss[N][N][2];
 
int main()
{
    int flag=0;
    int n,m;
    scanf("%d%d",&n,&m);
    S st;
    for(int i=0; i<n; i++)
    {
        scanf("%s",state[i]);
        for(int j=0; j<m; j++)
        {
            if(state[i][j]=='S')
            {
                st.x=i;
                st.y=j;
            }
        }
    }
    queue<S> Q;
    Q.push(st);
    vis[st.x][st.y]=1;
    viss[st.x][st.y][0]=st.x;
    viss[st.x][st.y][1]=st.y;
    while(!Q.empty())
    {
        S now=Q.front();
        S next;
        for(int i=0; i<4; i++)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            if(state[(next.x%n+n)%n][(next.y%m+m)%m]!='#')
            {
                if(vis[(next.x%n+n)%n][(next.y%m+m)%m]==1)
                {
                    if(viss[(next.x%n+n)%n][(next.y%m+m)%m][0]!=next.x||viss[(next.x%n+n)%n][(next.y%m+m)%m][1]!=next.y)
                    {
                        flag=1;
                        break;
                    }
                }
                else
                {
                    vis[(next.x%n+n)%n][(next.y%m+m)%m]=1;
                    viss[(next.x%n+n)%n][(next.y%m+m)%m][0]=next.x;
                    viss[(next.x%n+n)%n][(next.y%m+m)%m][1]=next.y;
                    Q.push(next);
                }
            }
        }
        if(flag==1)
            break;
        Q.pop();
    }
    if(flag==1)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值