|
- #include<iostream>
- #include<cstring>
- #define FOR(i,a,b) for(int i=a;i<=b;i++)
- using namespace std;
- const int M=1550;
- bool map[M][M];
- int vis[M][M][3],dx[4]={-1,0,+1,0},dy[4]={0,+1,0,-1};
- int n,m,sx,sy;
- char ch;
- bool ans;
- void dfs(int x,int y,int px,int py)
- {
- if(vis[px][py][2])
- {
- if(vis[px][py][0]!=x || vis[px][py][1]!=y)ans=1;
- return;
- }
- vis[px][py][2]=1;vis[px][py][0]=x;vis[px][py][1]=y;
- FOR(i,0,3)
- {
- register int tox=(px+dx[i]+n)%n;
- register int toy=(py+dy[i]+m)%m;
- if(map[tox][toy])dfs(x+dx[i],y+dy[i],tox,toy);
- }
- }
- int main()
- {
- while(cin>>n>>m)
- {
- memset(vis,0,sizeof(vis));
- ans=0;
- FOR(i,0,n-1)FOR(j,0,m-1){cin>>ch;if(ch=='.')map[i][j]=1;else map[i][j]=0;if(ch=='S'){sx=i;sy=j;map[i][j]=1;}}
- dfs(sx,sy,sx,sy);
- if(ans)cout<<"Yes"<<endl;
- else cout<<"No"<<endl;
- }
- return 0;
- }
复制代码 |
|