diff --git a/FloodFill.cpp b/FloodFill.cpp new file mode 100644 index 0000000..6407f77 --- /dev/null +++ b/FloodFill.cpp @@ -0,0 +1,53 @@ +#include +#define M 8 +#define N 8 +using namespace std; + +int screen[M][N] = { + {1, 1, 1, 1, 1, 1, 1, 1}, + {1, 1, 1, 1, 1, 1, 0, 0}, + {1, 0, 0, 1, 1, 0, 1, 1}, + {1, 2, 2, 2, 2, 0, 1, 0}, + {1, 1, 1, 2, 2, 0, 1, 0}, + {1, 1, 1, 2, 2, 2, 2, 0}, + {1, 1, 1, 1, 1, 2, 1, 1}, + {1, 1, 1, 1, 1, 2, 2, 1} +}; + +void fillScreen(int x, int y, int prevColor, int newColor) { + if (x < 0 || x >= M || y < 0 || y >= N) + return; + + if (screen[x][y] != prevColor) + return; + + screen[x][y] = newColor; + fillScreen(x+1, y, prevColor, newColor); + fillScreen(x-1, y, prevColor, newColor); + fillScreen(x, y+1, prevColor, newColor); + fillScreen(x, y-1, prevColor, newColor); + } + +void floodFill(int x, int y, int newColor) { + int prevColor = screen[x][y]; + fillScreen(x, y, prevColor, newColor); +} + +int main() { + int x = 4, y = 4, newColor = 3; + cout << "Previous screen: "<< endl; + for (int i=0; i