2 seconds
256 megabytes
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.
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.
Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes).
5 4
##.#
##S#
#..#
#.##
#..#
Yes
5 4
##.#
##S#
#..#
..#.
#.##
No
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的地图拼成的一个无限平面,
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
const int dir[][] = {,-,,,-,,,};
struct point{
int x,y;
};
char mp[maxn][maxn];
int vs[maxn][maxn][],r,c,sx,sy;
bool vis[maxn][maxn];
queue<point>q;
int mod(int u,bool rc){
if(rc) {u = u%r;if(u < ) u += r;}
else{u = u%c;if(u < ) u += c;}
return u;
}
bool bfs(){
while(!q.empty()) q.pop();
point pt = (point){sx,sy};
q.push(pt);
int i,j,x,y,mx,my;
memset(vs,,sizeof(vs));
while(!q.empty()){
point cur = q.front();
q.pop();
for(i = ; i < ; i++){
x = dir[i][]+cur.x;
y = dir[i][]+cur.y;
mx = mod(x,true);
my = mod(y,false);
if(mp[mx][my] == '#') continue;
if(vis[mx][my]){
if(x != vs[mx][my][] || y != vs[mx][my][]) return true;
}else{
vis[mx][my] = true;
vs[mx][my][] = x;
vs[mx][my][] = y;
q.push((point){x,y});
}
}
}
return false;
}
int main(){
int i,j;
while(~scanf("%d%d",&r,&c)){
for(i = ; i < r; i++){
scanf("%s",mp[i]);
for(j = ; j < c; j++)
if(mp[i][j] == 'S'){
sx = i;sy = j;
}
}
bfs()?puts("Yes"):puts("No");
}
return ;
}