CodeForces 196B Infinite Maze

题解:

196B - Infinite Maze

Answer is "Yes" iff there are two distinct, reachable from start position cells, which correspond to same cell in initial labyrinth.

Proof: If these cells exist, move to first of them, and infinitely repeat moves leading from first to second. On the contrary, if infinite far path exist, on this path we obviously can find such cells.

How to find out if they exist? Start DFS from initial cell. For each cell visited, let visit[x%n][y%m] = (x, y). Now, if DFS tries to go to cell (x, y)visit[x%n][y%m] contains something, and (x, y) ≠ visit[x%n][y%m], we found these cells: they are (x, y) and visit[x%n][y%m]. Notice that DFS will visit no more than nm + 1 cells (Dirichlet's principle). So the asymptotic is O(nm).

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).

Sample test(s)
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.


#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define ll long long
#define prt(k) cout<<#k"="<<k<<" "
const int inf=1e9;
const int N=1555;
int vis[N][N][3];
int n,m;
char a[N][N];
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
bool dfs(int x,int y)
{
    int xx=(x%n+n)%n,yy=(y%m+m)%m;
    if(a[xx][yy]=='#') return 0;
    if(vis[xx][yy][0]!=-inf)
        return vis[xx][yy][0]!=x||vis[xx][yy][1]!=y;
    vis[xx][yy][0]=x,vis[xx][yy][1]=y;
    for(int i=0;i<4;i++) if(dfs(x+dx[i],y+dy[i])) return 1;
    return 0;
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++) cin>>a[i];
    int sx=0,sy=0;
    for(int i=0;i<n;i++) for(int j=0;j<m;j++) { vis[i][j][0]=-inf; if(a[i][j]=='S') sx=i,sy=j; }
    if(dfs(sx,sy)) puts("Yes");
    else puts("No");
}



你可能感兴趣的:(数据结构,算法,动态规划,codeforces,比赛)