1. 程式人生 > >[CodeForces - 197D] D - Infinite Maze

[CodeForces - 197D] D - Infinite Maze

little har rep print class pair align pri ati

D - Infinite Maze

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

Example

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.

題目的大意就是,給一張網格圖,某些地方可以走,其余的則不行,然後某個人從某個點出發,一直在迷宮走,如果走出邊界,則回到這個迷宮內相應的地方(當然要可以走),問你是否能走到一個"新的"起點位置.

一開始,我以為這題很水,DFS一遍就好,在四個邊界上,上下,左右的同一個位置,找一下是否都能從起點訪問到,就輸出yes.後面發現這個想法太naive了,好的反例能hack掉,又加了一道,但是又被hack...

然後失去了信心.到比賽結束後才發現反例,然後很難改,於是換了一種思路,直接根據題意進行模擬就好了,知道滿足要求,然後竟然就過了...qwq

技術分享
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define mp make_pair
 5 using namespace std;
 6 const int maxn=1505,fl[4][2]={{0,1},{1,0},{-1,0},{0,-1}};
 7 int n,m,Sx,Sy;
 8 pair<int,int> vis[maxn][maxn];
 9 char c[maxn][maxn];
10 bool v[maxn][maxn];
11 bool jug(int x,int y){return x>-1&&x<n&&y>-1&&y<m&&c[x][y]!=#&&!v[x][y];}
12 bool DFS(int x,int y){
13     int xx=x,yy=y;
14     while (xx<0) xx+=n; xx%=n;
15     while (yy<0) yy+=m; yy%=m;
16     if (v[xx][yy]&&(vis[xx][yy].first!=x||vis[xx][yy].second!=y)){puts("Yes"); exit(0);}
17     if (!jug(xx,yy)) return 0;
18     v[xx][yy]=1,vis[xx][yy]=mp(x,y);
19     for (int i=0; i<4; i++) DFS(x+fl[i][0],y+fl[i][1]);
20 }
21 int main(){
22     scanf("%d%d",&n,&m); char s[1505];
23     for (int i=0; i<n; i++){
24         scanf("%s",s); for (int j=0; j<m; j++){
25             c[i][j]=s[j];
26             if (c[i][j]==S) Sx=i,Sy=j;
27         }
28     }
29     if (DFS(Sx,Sy)) puts("Yes"); else puts("No");
30     return 0;
31 }
View Code

[CodeForces - 197D] D - Infinite Maze