zoukankan      html  css  js  c++  java
  • Infinite Maze CodeForces

    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;
    }
  • 相关阅读:
    【sping揭秘】18、使用spring访问数据
    【sping揭秘】17、@Around,@Introduction
    【sping揭秘】16、@After(finally) 但是这个实在afterturning之前执行
    【sping揭秘】15、afterreturning
    【sping揭秘】14、@before @AfterThrowing
    【sping揭秘】13、Spring AOP一世&二世
    【sping揭秘】12、SpringAOP的实现机制
    【sping揭秘】11、Java 平台上的AOP实现机制
    【sping揭秘】10、SpringIOC容器扩展
    nginx做为web容器部署静态资源以及做负载均衡反向代理实现
  • 原文地址:https://www.cnblogs.com/smallhester/p/10397888.html
Copyright © 2011-2022 走看看