首页 > 代码库 > [CodeForces - 197D] D - Infinite Maze
[CodeForces - 197D] D - Infinite Maze
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
5 4
##.#
##S#
#..#
#.##
#..#
Yes
5 4
##.#
##S#
#..#
..#.
#.##
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 }
[CodeForces - 197D] D - Infinite Maze