7 questions. Covers Flood fill.
Questions 1 to 5 of 7
cpp int fillLate(vector<string>& grid, int r, int c) { if (r < 0 || r >= (int)grid.size()) return 0; if (c < 0 || c >= (int)grid[r].size()) return 0; if (grid[r][c] != '.') return 0; int count = 1; count += fillLate(grid, r - 1, c); count += fillLate(grid, r + 1, c); count += fillLate(grid, r, c - 1); count += fillLate(grid, r, c + 1); grid[r][c] = '*'; return count; }
>=
.
cpp int fill(vector<string>& grid, int r, int c, char open) { if (r < 0 || r >= (int)grid.size()) return 0; if (c < 0 || c >= (int)grid[r].size()) return 0; grid[r][c] = '*'; int count = 1; count += fill(grid, r - 1, c, open); count += fill(grid, r + 1, c, open); count += fill(grid, r, c - 1, open); count += fill(grid, r, c + 1, open); return count; }
grid[r][c] = '*';
Use the arrows to put the steps in order.
cpp #include <iostream> #include <string> #include <vector> using namespace std; int fill(vector<string>& grid, int r, int c, char open) { if (r < 0 || r >= (int)grid.size()) return 0; if (c < 0 || c >= (int)grid[r].size()) return 0; if (grid[r][c] != open) return 0; grid[r][c] = '*'; int count = 1; count += fill(grid, r - 1, c, open); count += fill(grid, r + 1, c, open); count += fill(grid, r, c - 1, open); count += fill(grid, r, c + 1, open); return count; } int main() { vector<string> grid = {"..#", ".#.", "..."}; int n = fill(grid, 0, 0, '.'); cout << n << "\n"; for (const string& row : grid) cout << row << "\n"; }
3 question(s) left on this page.