Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- using namespace std;
- // Directions: up, down, top-left, bottom-right
- int dx[] = {-1, 1, -1, 1};
- int dy[] = {0, 0, -1, 1};
- int main() {
- int n, x, y;
- cin >> n >> x >> y;
- vector<vector<int>> maze(n, vector<int>(n));
- vector<vector<bool>> visited(n, vector<bool>(n, false));
- // Read the maze
- for (int i = 0; i < n; ++i)
- for (int j = 0; j < n; ++j)
- cin >> maze[i][j];
- // Check if starting point is blocked
- if (maze[x][y] == 1) {
- cout << 0 << endl;
- return 0;
- }
- int count = 0;
- queue<pair<int, int>> q;
- q.push({x, y});
- visited[x][y] = true;
- while (!q.empty()) {
- auto [cx, cy] = q.front(); q.pop();
- count++;
- for (int i = 0; i < 4; ++i) {
- int nx = cx + dx[i];
- int ny = cy + dy[i];
- if (nx >= 0 && nx < n && ny >= 0 && ny < n &&
- !visited[nx][ny] && maze[nx][ny] == 0) {
- visited[nx][ny] = true;
- q.push({nx, ny});
- }
- }
- }
- cout << count << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement