diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6a85229 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++.exe build active file", + "command": "C:\\MinGW\\bin\\g++.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/C++/FloodFillDFS.cpp b/C++/FloodFillDFS.cpp new file mode 100644 index 0000000..f8dc471 --- /dev/null +++ b/C++/FloodFillDFS.cpp @@ -0,0 +1,52 @@ +#include +#include +using namespace std; + +class FloodFillDFS { +public: + void floodFill(vector>& image, int sr, int sc, int newColor) { + int oldColor = image[sr][sc]; + if (oldColor == newColor) return; + dfs(image, sr, sc, oldColor, newColor); + } + + void dfs(vector>& image, int r, int c, int oldColor, int newColor) { + // Boundary check + if (r < 0 || c < 0 || r >= static_cast(image.size()) || c >= static_cast(image[0].size())) + return; + + // Stop if color does not match + if (image[r][c] != oldColor) + return; + + // Replace color + image[r][c] = newColor; + + // Explore neighbors + dfs(image, r + 1, c, oldColor, newColor); + dfs(image, r - 1, c, oldColor, newColor); + dfs(image, r, c + 1, oldColor, newColor); + dfs(image, r, c - 1, oldColor, newColor); + } +}; + +int main() { + vector> image = { + {1, 1, 1}, + {1, 1, 0}, + {1, 0, 1} + }; + + FloodFillDFS solver; + solver.floodFill(image, 1, 1, 2); + + // Print output + for (auto& row : image) { + for (int val : row) { + cout << val << " "; + } + cout << endl; + } + + return 0; +} diff --git a/README_FLOOD_FILL.md b/README_FLOOD_FILL.md new file mode 100644 index 0000000..73d9048 --- /dev/null +++ b/README_FLOOD_FILL.md @@ -0,0 +1,5 @@ +## Flood Fill Algorithm (LeetCode 733) + +- Uses DFS to fill connected cells of the same color. +- Time: O(R × C) +- Space: O(R × C) diff --git a/java/FloodFillDFS.java b/java/FloodFillDFS.java new file mode 100644 index 0000000..4229934 --- /dev/null +++ b/java/FloodFillDFS.java @@ -0,0 +1,40 @@ +public final class FloodFillDFS { + + private FloodFillDFS() { + // Utility class; prevent instantiation. + } + public static void main(String[] args) { + int[][] image = { + {1, 1, 1}, + {1, 1, 0}, + {1, 0, 1} + }; + + floodFill(image, 1, 1, 2); + + for (int[] row : image) { + for (int val : row) { + System.out.print(val + " "); + } + System.out.println(); + } + } + + static void floodFill(int[][] image, int sr, int sc, int newColor) { + int oldColor = image[sr][sc]; + if (oldColor == newColor) return; + dfs(image, sr, sc, oldColor, newColor); + } + + static void dfs(int[][] image, int r, int c, int oldColor, int newColor) { + if (r < 0 || c < 0 || r >= image.length || c >= image[0].length) return; + if (image[r][c] != oldColor) return; + + image[r][c] = newColor; + + dfs(image, r + 1, c, oldColor, newColor); + dfs(image, r - 1, c, oldColor, newColor); + dfs(image, r, c + 1, oldColor, newColor); + dfs(image, r, c - 1, oldColor, newColor); + } +}