Embercode
✕
GraphsQuick quiz

Grid search and state BFS

8 questions. Covers State-space BFS with keys and doors.

Questions 1 to 5 of 8

  1. 1. What does this program print? It counts islands with four-way moves, where 1 is land and 0 is water. ``cpp #include <iostream> #include <string> #include <vector> std::vector<std::string> g = {"110", "011", "100", "001"}; void sink(int r, int c) { if (r < 0 || r >= 4 || c < 0 || c >= 3 || g[r][c] != '1') return; g[r][c] = '0'; sink(r - 1, c); sink(r + 1, c); sink(r, c - 1); sink(r, c + 1); } int main() { int count = 0; for (int r = 0; r < 4; ++r) for (int c = 0; c < 3; ++c) if (g[r][c] == '1') { ++count; sink(r, c); } std::cout << count << '\n'; } ``
  2. 2. What does this program print? ``cpp #include <iostream> #include <string> int main() { int keys = 0; for (char ch : std::string("ace")) keys |= 1 << (ch - 'a'); std::cout << keys << ' ' << (keys & (1 << ('c' - 'a'))) << ' ' << (keys & (1 << ('b' - 'a'))) << '\n'; } ``
  3. 3. In floodFill, the first lines are char oldColor = g[sr][sc]; and if (oldColor == newColor) return;. What happens if that guard is removed and the fill is called with the same colour twice?
  4. 4. The lesson's third maze gives 6 when the visited index includes the key set. Which change makes the same maze return -1?
  5. 5. The grid below has three land cells. How many islands does it have with four-way moves, and how many with eight-way moves? ``text 110 001 ``

5 question(s) left on this page.