8 int nearestExit(vector<vector<char>>& maze, vector<int>& entrance)
10 int m = (int)maze.size(), n = (int)maze[0].size();
12 vector<vector<bool>> visited(m, vector(n,
false));
15 int x = entrance[0], y = entrance[1];
19 queue<pair<int, int>> steps;
20 if (x - 1 >= 0 && maze[x - 1][y] ==
'.')
21 steps.push({x - 1, y});
22 if (x + 1 < m && maze[x + 1][y] ==
'.')
23 steps.push({x + 1, y});
24 if (y - 1 >= 0 && maze[x][y - 1] ==
'.')
25 steps.push({x, y - 1});
26 if (y + 1 < n && maze[x][y + 1] ==
'.')
27 steps.push({x, y + 1});
33 while (!steps.empty())
35 int size = steps.size();
36 for (
int i = 0; i < size; ++i)
38 auto [x, y] = steps.front();
42 if (x == 0 || y == 0 || x == m - 1 || y == n - 1)
45 if (x - 1 >= 0 && maze[x - 1][y] ==
'.' && !(visited[x - 1][y]))
47 steps.push({x - 1, y});
48 visited[x - 1][y] =
true;
50 if (x + 1 < m && maze[x + 1][y] ==
'.' && !(visited[x + 1][y]))
52 steps.push({x + 1, y});
53 visited[x + 1][y] =
true;
55 if (y - 1 >= 0 && maze[x][y - 1] ==
'.' && !(visited[x][y - 1]))
57 steps.push({x, y - 1});
58 visited[x][y - 1] =
true;
60 if (y + 1 < n && maze[x][y + 1] ==
'.' && !(visited[x][y + 1]))
62 steps.push({x, y + 1});
63 visited[x][y + 1] =
true;