Infinite Maze CodeForces - 196B

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.

题意:给了一个迷宫,迷宫中只有路的地方可以走,在迷宫的四周也都布满了一样的迷宫, 问可不可以一直无限的走下去

思路:在走迷宫的时候用两个坐标来标记,一个是实际的坐标(可能会有负的,可能会有超过n和m的),还有一个是对应到题目给的迷宫上的坐标,设走的点的实际的坐标是nx和ny,那么这个坐标对应在题目给的迷宫里面的坐标的就是(nx%n+n)%n和(ny%m+m)%m,在搜索的时候通过记录的实际的坐标点的结构体来判断这个点的对应在题目给的图里面有没有对应的,如果有的话就说明这个点已经走过了,如果在走过的情况下并且已经走过两次了,那么就说明这个是可以无限的走的,没有的话那么就要把这个实际的坐标点放进去,用来判断接下去会不会走过这些点

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<cstdlib>
#include <vector>
#include <set>
#include<queue>
#include<map>
using namespace std;

#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
const int maxn =  1505;
const ll mod = 1e9+7;
const double eps = 1e-8;

struct node
{
    int x,y;
    bool operator == (const node &a) const {
        return x==a.x && y==a.y;
    }
};
int n,m;
char a[maxn][maxn];
node  vis[maxn][maxn];
int v[maxn][maxn];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
bool bfs(int sx,int sy)
{
    memset(v,0,sizeof v);
    queue<node>que;
    v[sx][sy] = true;
    vis[sx][sy] = node{sx,sy};
    que.push(node{sx,sy});
    while(que.size())
    {
        node p = que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int nx = p.x + dx[i];
            int ny = p.y + dy[i];
            int rx = (nx%n+n)%n;
            int ry = (ny%m+m)%m;
            if(a[rx][ry] == '#')
                continue;
            node next = node {nx,ny};
            if(v[rx][ry])
            {
                if(!(next == vis[rx][ry]))
                    return true;
            }
            else
            {
                v[rx][ry] = true;
                vis[rx][ry] = next;
                que.push(node{nx,ny});
            }
        }
    }
    return false;
}
int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
        cin>>a[i];
    int sx,sy;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(a[i][j] == 'S')
            {
                sx = i;
                sy = j;
            }
        }
    }
    if(bfs(sx,sy))
        puts("Yes");
    else
        puts("No");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
infinite-scroll-immediate是一个属性,用于控制是否立即执行加载方法,以防初始状态下内容无法撑满容器。默认情况下,infinite-scroll-immediate的值是false,即不会立即执行加载方法。如果需要在初始状态下就执行加载方法,可以将infinite-scroll-immediate的值设置为true。这样,在组件初始化时就会执行加载方法。\[2\] #### 引用[.reference_title] - *1* [vue-infinite-scroll无限滚动组件](https://blog.csdn.net/weixin_45807026/article/details/125621351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Element UI - v-infinite-scroll无限滚动组件](https://blog.csdn.net/m0_70547044/article/details/128517328)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【Vue】实现无限滚动加载](https://blog.csdn.net/NineWaited/article/details/126387768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值