queue
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
std::queue is a queue: to the back (push), from the front (front, pop). First in, first out (FIFO).
#include <queue>
std::queue<std::string> q;
q.push("Kai"); q.push("Alex");
q.front(); // "Kai"
q.pop(); // now "Alex" is first
Where you meet it: the event queue in a game, jobs on a server, breadth-first search (BFS) — finding the shortest path on a grid: from the starting cell we put its neighbours into the queue, then their neighbours, and so on, like a "wave".
There's also std::priority_queue — the largest item comes out first (by default), not the earliest one.
Task. The shortest path on a map. The first line is h w, followed by h lines of . (passage), # (wall),
S (start), E (exit). You can move up, down, left and right. Print the length of the shortest path from S to E,
or -1 if there's no path. Use BFS with a std::queue.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.