xtu summer individual 3 C.Infinite Maze

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.

 

解题:无限地图无限搜,只要可以搜到一个前面已经访问过的点,但是,并不在同一幅地图里面!!!因为可以看成是很多块这样n*m的地图拼成的一个无限平面,

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #define LL long long

13 #define INF 0x3f3f3f3f

14 using namespace std;

15 const int maxn = 1510;

16 const int dir[4][2] = {0,-1,0,1,-1,0,1,0};

17 struct point{

18     int x,y;

19 };

20 char mp[maxn][maxn];

21 int vs[maxn][maxn][2],r,c,sx,sy;

22 bool vis[maxn][maxn];

23 queue<point>q;

24 int mod(int u,bool rc){

25     if(rc) {u = u%r;if(u < 0) u += r;}

26     else{u = u%c;if(u < 0) u += c;}

27     return u;

28 }

29 bool bfs(){

30     while(!q.empty()) q.pop();

31     point pt = (point){sx,sy};

32     q.push(pt);

33     int i,j,x,y,mx,my;

34     memset(vs,0,sizeof(vs));

35     while(!q.empty()){

36         point cur = q.front();

37         q.pop();

38         for(i = 0; i < 4; i++){

39             x = dir[i][0]+cur.x;

40             y = dir[i][1]+cur.y;

41             mx = mod(x,true);

42             my = mod(y,false);

43             if(mp[mx][my] == '#') continue;

44             if(vis[mx][my]){

45                 if(x != vs[mx][my][0] || y != vs[mx][my][1]) return true;

46             }else{

47                 vis[mx][my] = true;

48                 vs[mx][my][0] = x;

49                 vs[mx][my][1] = y;

50                 q.push((point){x,y});

51             }

52         }

53     }

54     return false;

55 }

56 int main(){

57     int i,j;

58     while(~scanf("%d%d",&r,&c)){

59         for(i = 0; i < r; i++){

60             scanf("%s",mp[i]);

61             for(j = 0; j < c; j++)

62             if(mp[i][j] == 'S'){

63                 sx = i;sy = j;

64             }

65         }

66         bfs()?puts("Yes"):puts("No");

67     }

68     return 0;

69 }
View Code

 

 

你可能感兴趣的:(init)