//题意描述,给你一张最大1500*1500的图,途中的S称为起点,图可以无限延伸,意思是能够将图能够无限平移,最后让你判断,从起点处能否到达无穷远;
//思路,如果一个图能从起点到达无穷远,一定能从图的任意一点到达下一张图与原图对应的点。那么代码就很好写了。
1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 1505; 7 const int dx[]= {1,-1,0,0}; //方向 8 const int dy[]= {0,0,-1,1}; //方向 9 char str[maxn][maxn]; 10 int vis[maxn][maxn][2]; //存当前点的实际坐标 11 bool flag[maxn][maxn]; //记录该店是否被访问过 12 int n,m; 13 struct node //结构体 14 { 15 int x,y; 16 }; 17 bool bfs(node now) 18 { 19 int i; 20 node next,inthismap; 21 memset(flag,false,sizeof(flag)); 22 queue<node>que; 23 que.push(now); //将起点加入队列 24 while(!que.empty()) 25 { 26 now = que.front(); 27 que.pop(); 28 for(i=0;i<4;i++) 29 { 30 next.x = now.x+dx[i]; 31 next.y = now.y+dy[i]; 32 inthismap.x = (next.x%n+n)%n; 33 inthismap.y = (next.y%m+m)%m; 34 if(str[inthismap.x][inthismap.y]=='#') 35 continue; 36 if(flag[inthismap.x][inthismap.y]) 37 { 38 if(next.x!=vis[inthismap.x][inthismap.y][0]||next.y!=vis[inthismap.x][inthismap.y][1]) 39 return true; 40 } 41 else 42 { 43 flag[inthismap.x][inthismap.y]=true; 44 vis[inthismap.x][inthismap.y][0]=next.x; 45 vis[inthismap.x][inthismap.y][1]=next.y; 46 que.push(next); 47 } 48 } 49 } 50 return false; 51 } 52 int main() 53 { 54 55 node start; 56 int i,j; 57 while(scanf("%d%d",&n,&m)!=EOF) 58 { 59 for(i=0;i<n;i++) 60 { 61 scanf("%s",str[i]); 62 for(j=0;j<m;j++) 63 if(str[i][j]=='S') 64 start.x=i,start.y=j; 65 } 66 if(bfs(start)) 67 printf("Yes\n"); 68 else 69 printf("No\n"); 70 } 71 return 0; 72 } 73 74