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 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <bits/stdc++.h> using namespace std;
const int MAXN = 105; char maze[MAXN][MAXN]; bool visited[MAXN][MAXN]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int n, m;
bool bfs(int x, int y) { queue<pair<int, int>> q; q.push({x, y}); visited[x][y] = true;
while (!q.empty()) { pair<int, int> p = q.front(); q.pop();
if (p.first == n - 1 && p.second == m - 1) { return true; }
for (int i = 0; i < 4; i++) { int nx = p.first + dx[i]; int ny = p.second + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny] && maze[nx][ny] == '.') { visited[nx][ny] = true; q.push({nx, ny}); } } }
return false; }
int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> maze[i][j]; } }
if (bfs(0, 0)) { cout << "Yes" << endl; } else { cout << "No" << endl; }
return 0; }
|