Flood Fill
Completed
Read a grid of . and # and a start cell. Recursively fill the connected . region containing the start (4 directions) with o, and print the grid and the region size.
Input
r c, the rows, then the start row and column.
Output
The grid, then the size.
Example
Input
4 5 ..#.. .##.. #.... ..#.# 0 0
Output
oo#.. o##.. #.... ..#.# 3
Editor
C++
Hints
Mark a cell before recursing so it isn't visited twice.
Solution explanation
Flood fill is how paint programs fill an area.
Show solution
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> grid;
int R, C;
int fill(int r, int c) {
if (r < 0 || r >= R || c < 0 || c >= C || grid[r][c] != '.') return 0;
grid[r][c] = 'o';
return 1 + fill(r + 1, c) + fill(r - 1, c) + fill(r, c + 1) + fill(r, c - 1);
}
int main() {
cin >> R >> C;
grid.resize(R);
for (auto &row : grid) cin >> row;
int sr, sc;
cin >> sr >> sc;
int size = fill(sr, sc);
for (const auto &row : grid) cout << row << "\n";
cout << size << "\n";
return 0;
}