Problem solution · C++

ABC007 C — 幅優先探索

ABC007 C — 幅優先探索: 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
76 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 ABC007 C — 幅優先探索, 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

  • 76 lines of C++ from the credited upstream file abc007_3.cpp.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 3 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 codeABC007 C — 幅優先探索 · C++C++
Use this to learn the idea, then write your own version.
#include <cmath>#include <iostream>#include <map>#include <tuple>#include <vector> using namespace std; string to_key(unsigned int y, unsigned int x) {    return to_string(y) + "-" + to_string(x);} void bfs(vector<vector<char>>& grid,         vector<tuple<unsigned int, unsigned int>> cands, unsigned int l,         vector<vector<unsigned int>>& visited) {    if (cands.size() == 0) {        return;    }     vector<tuple<unsigned int, unsigned int>> new_cands;    map<string, bool> cands_map;    for (auto& c : cands) {        unsigned int y = get<0>(c);        unsigned int x = get<1>(c);         grid[y][x] = '#';        visited[y][x] = min(visited[y][x], l);         if (grid[y - 1][x] != '#' && !cands_map[to_key(y - 1, x)]) {            new_cands.push_back({y - 1, x});            cands_map[to_key(y - 1, x)] = true;        }         if (grid[y][x - 1] != '#' && !cands_map[to_key(y, x - 1)]) {            new_cands.push_back({y, x - 1});            cands_map[to_key(y, x - 1)] = true;        }         if (grid[y + 1][x] != '#' && !cands_map[to_key(y + 1, x)]) {            new_cands.push_back({y + 1, x});            cands_map[to_key(y + 1, x)] = true;        }         if (grid[y][x + 1] != '#' && !cands_map[to_key(y, x + 1)]) {            new_cands.push_back({y, x + 1});            cands_map[to_key(y, x + 1)] = true;        }    }     bfs(grid, new_cands, l + 1, visited);} int main() {    unsigned int r, c;    cin >> r >> c;     unsigned int sy, sx;    cin >> sy >> sx;     unsigned int gy, gx;    cin >> gy >> gx;     vector<vector<char>> grid(r + 2, vector<char>(c + 2, '#'));    for (unsigned int y = 0; y < r; y++) {        for (unsigned int x = 0; x < c; x++) {            cin >> grid[y + 1][x + 1];        }    }     vector<vector<unsigned int>> visited(r + 2,                                         vector<unsigned int>(c + 2, r * c));    vector<tuple<unsigned int, unsigned int>> cands = {{sy, sx}};    bfs(grid, cands, 0, visited);     cout << visited[gy][gx] << endl;}

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 ↗