6 questions. Covers Explicit stack.
Questions 1 to 5 of 6
Use the arrows to put the steps in order.
push_back
empty()
back()
pop_back()
'.'
'o'
cpp #include <iostream> #include <vector> int main() { std::vector<int> pending; pending.push_back(1); pending.push_back(2); pending.push_back(3); pending.pop_back(); pending.push_back(4); while (!pending.empty()) { std::cout << pending.back() << ' '; pending.pop_back(); } std::cout << "\n"; }
cpp #include <iostream> #include <string> #include <vector> using Grid = std::vector<std::string>; struct Cell { int row; int col; }; bool inside(const Grid& grid, int row, int col) { return row >= 0 && row < static_cast<int>(grid.size()) && col >= 0 && col < static_cast<int>(grid[row].size()); } int main() { Grid grid = { "...", ".#.", }; std::vector<Cell> pending; pending.push_back({0, 0}); while (!pending.empty()) { Cell cell = pending.back(); pending.pop_back(); if (!inside(grid, cell.row, cell.col) || grid[cell.row][cell.col] != '.') { continue; } grid[cell.row][cell.col] = 'o'; std::cout << "fill (" << cell.row << "," << cell.col << ")\n"; pending.push_back({cell.row + 1, cell.col}); pending.push_back({cell.row - 1, cell.col}); pending.push_back({cell.row, cell.col + 1}); pending.push_back({cell.row, cell.col - 1}); } }
cpp #include <iostream> #include <vector> struct Cell { int row; int col; }; int main() { std::vector<Cell> pending; pending.push_back({0, 0}); pending.push_back({1, 0}); while (!pending.empty()) { pending.pop_back(); Cell cell = pending.back(); std::cout << cell.row << "," << cell.col << "\n"; } }
while
size() > 1
Cell
{0, 0}
3 question(s) left on this page.