1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <bits/stdc++.h> using namespace std; const int LEN = 2500; int n, m, walk[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; bool ans; char arr[LEN][LEN]; struct node { bool vis_; int nx_, ny_; } vis[LEN][LEN];
void dfs(int x, int y, int nx, int ny) { if (ans) return; x = (x + n) % n, y = (y + m) % m; if (arr[x][y] == '#') return; if (vis[x][y].vis_) { if ((vis[x][y].nx_ != nx || vis[x][y].ny_ != ny)) ans = true; return; } vis[x][y].vis_ = 1, vis[x][y].nx_ = nx, vis[x][y].ny_ = ny; for (int i = 0; i < 4; ++i) { dfs(x + walk[i][0], y + walk[i][1], nx + walk[i][0], ny + walk[i][1]); } }
int main() { int x, y; while (cin >> n >> m) { ans = false; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { cin >> arr[i][j]; if (arr[i][j] == 'S') x = i, y = j; } dfs(x, y, x, y); cout << (ans ? "Yes" : "No") << endl; } return 0; }
|