Problem solution · C++

ABC405 D — Escape Route

ABC405 D — Escape Route: a C++ solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to michimani AtCoder Solutions.

Technique
Breadth-first search
Source
michimani AtCoder Solutions
Length
68 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For ABC405 D — Escape Route, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 68 lines of C++ from the credited upstream file abc405_d.cpp.
  • The implementation visibly relies on sequence storage, work queue.
  • 8 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from michimani AtCoder Solutions by michimani and is used under the MIT licence.

Full codeABC405 D — Escape Route · C++C++
Use this to learn the idea, then write your own version.
#include <iostream>#include <queue>#include <vector> using namespace std;using ui = unsigned int; int main() {    ui h, w;    cin >> h >> w;     vector<string> g(h, string(w, '.'));    vector<vector<int>> dist(h, vector<int>(w, -1));    queue<pair<ui, ui>> q;     for (ui i = 0; i < h; i++) {        for (ui j = 0; j < w; j++) {            cin >> g[i][j];            if (g[i][j] == 'E') {                q.push({i, j});                dist[i][j] = 0;            }        }    }     vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};    while (!q.empty()) {        auto [i, j] = q.front();        q.pop();         for (auto [di, dj] : dirs) {            int ni = int(i) + di, nj = int(j) + dj;            if (ni < 0 || ni >= int(h) || nj < 0 || nj >= int(w)) {                continue;            }            if (g[ui(ni)][ui(nj)] == '.' && dist[ui(ni)][ui(nj)] == -1) {                dist[ui(ni)][ui(nj)] = dist[i][j] + 1;                q.push({ni, nj});            }        }    }     vector<string> ans(h, string(w, '#'));    vector<char> dest = {'^', 'v', '<', '>'};    for (ui i = 0; i < h; i++) {        for (ui j = 0; j < w; j++) {            if (g[i][j] == '.') {                for (ui k = 0; k < 4; k++) {                    auto [di, dj] = dirs[k];                    int ni = int(i) + di, nj = int(j) + dj;                    if (ni < 0 || ni >= int(h) || nj < 0 || nj >= int(w)) {                        continue;                    }                    if (dist[ui(ni)][ui(nj)] == dist[i][j] - 1) {                        ans[i][j] = dest[k];                        break;                    }                }            } else if (g[i][j] == 'E') {                ans[i][j] = 'E';            }        }    }     for (const auto& row : ans) cout << row << endl;     return 0;}

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗