Below is the implementation of the digital paint bucket tool:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101// Recursive DFS Approachfunction floodFill(canvas, startRow, startCol, newColor) { // Get the original color const originalColor = canvas[startRow][startCol]; // If the starting pixel is already the new color, no need to do anything if (originalColor === newColor) { return canvas; } // Call the recursive helper function dfs(canvas, startRow, startCol, originalColor, newColor); return canvas;} function dfs(canvas, row, col, originalColor, newColor) { // Check if the current position is valid if (row < 0 || row >= canvas.length || col < 0 || col >= canvas[0].length) { return; } // Check if the current pixel is the original color if (canvas[row][col] !== originalColor) { return; } // Change the color of the current pixel canvas[row][col] = newColor; // Recursively fill the adjacent pixels dfs(canvas, row + 1, col, originalColor, newColor); // Down dfs(canvas, row - 1, col, originalColor, newColor); // Up dfs(canvas, row, col + 1, originalColor, newColor); // Right dfs(canvas, row, col - 1, originalColor, newColor); // Left} // Iterative BFS Approachfunction floodFillBFS(canvas, startRow, startCol, newColor) { // Get the original color const originalColor = canvas[startRow][startCol]; // If the starting pixel is already the new color, no need to do anything if (originalColor === newColor) { return canvas; } // Initialize a queue with the starting pixel const queue = [[startRow, startCol]]; while (queue.length > 0) { const [row, col] = queue.shift(); // Check if the current position is valid if (row < 0 || row >= canvas.length || col < 0 || col >= canvas[0].length) { continue; } // Check if the current pixel is the original color if (canvas[row][col] !== originalColor) { continue; } // Change the color of the current pixel canvas[row][col] = newColor; // Enqueue the adjacent pixels queue.push([row + 1, col]); // Down queue.push([row - 1, col]); // Up queue.push([row, col + 1]); // Right queue.push([row, col - 1]); // Left } return canvas;} // Test casesconst canvas1 = [ ['R', 'R', 'R', 'R'], ['R', 'B', 'B', 'R'], ['R', 'B', 'B', 'R'], ['R', 'R', 'R', 'R']]; console.log("Original Canvas:");printCanvas(canvas1); console.log("\nAfter Flood Fill (DFS):");const filledCanvas1 = floodFill([...canvas1.map(row => [...row])], 1, 1, 'G');printCanvas(filledCanvas1); console.log("\nAfter Flood Fill (BFS):");const filledCanvas2 = floodFillBFS([...canvas1.map(row => [...row])], 1, 1, 'G');printCanvas(filledCanvas2); // Helper function to print the canvasfunction printCanvas(canvas) { for (const row of canvas) { console.log(row.join(' ')); }}
Let's break down the implementation:
Implement the digital paint bucket tool solution in different programming languages.
Below is the implementation of the digital paint bucket tool in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101// Recursive DFS Approachfunction floodFill(canvas, startRow, startCol, newColor) { // Get the original color const originalColor = canvas[startRow][startCol]; // If the starting pixel is already the new color, no need to do anything if (originalColor === newColor) { return canvas; } // Call the recursive helper function dfs(canvas, startRow, startCol, originalColor, newColor); return canvas;} function dfs(canvas, row, col, originalColor, newColor) { // Check if the current position is valid if (row < 0 || row >= canvas.length || col < 0 || col >= canvas[0].length) { return; } // Check if the current pixel is the original color if (canvas[row][col] !== originalColor) { return; } // Change the color of the current pixel canvas[row][col] = newColor; // Recursively fill the adjacent pixels dfs(canvas, row + 1, col, originalColor, newColor); // Down dfs(canvas, row - 1, col, originalColor, newColor); // Up dfs(canvas, row, col + 1, originalColor, newColor); // Right dfs(canvas, row, col - 1, originalColor, newColor); // Left} // Iterative BFS Approachfunction floodFillBFS(canvas, startRow, startCol, newColor) { // Get the original color const originalColor = canvas[startRow][startCol]; // If the starting pixel is already the new color, no need to do anything if (originalColor === newColor) { return canvas; } // Initialize a queue with the starting pixel const queue = [[startRow, startCol]]; while (queue.length > 0) { const [row, col] = queue.shift(); // Check if the current position is valid if (row < 0 || row >= canvas.length || col < 0 || col >= canvas[0].length) { continue; } // Check if the current pixel is the original color if (canvas[row][col] !== originalColor) { continue; } // Change the color of the current pixel canvas[row][col] = newColor; // Enqueue the adjacent pixels queue.push([row + 1, col]); // Down queue.push([row - 1, col]); // Up queue.push([row, col + 1]); // Right queue.push([row, col - 1]); // Left } return canvas;} // Test casesconst canvas1 = [ ['R', 'R', 'R', 'R'], ['R', 'B', 'B', 'R'], ['R', 'B', 'B', 'R'], ['R', 'R', 'R', 'R']]; console.log("Original Canvas:");printCanvas(canvas1); console.log("\nAfter Flood Fill (DFS):");const filledCanvas1 = floodFill([...canvas1.map(row => [...row])], 1, 1, 'G');printCanvas(filledCanvas1); console.log("\nAfter Flood Fill (BFS):");const filledCanvas2 = floodFillBFS([...canvas1.map(row => [...row])], 1, 1, 'G');printCanvas(filledCanvas2); // Helper function to print the canvasfunction printCanvas(canvas) { for (const row of canvas) { console.log(row.join(' ')); }}
The flood fill algorithm changes the color of a pixel and all adjacent pixels of the same color, continuing until no more connected pixels of the original color remain.
Recognize that this is a classic DFS problem where we recursively explore all adjacent pixels of the same color.
The base cases are when the current position is out of bounds, or the current pixel is not the original color, or it's already the new color.
Write a function that handles the base cases and recursively explores the adjacent pixels, changing their colors as we go.
Implement an iterative BFS solution using a queue, which can be more efficient for very large canvases.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the digital paint bucket tool problem using a different approach than shown above.
The algorithm should work correctly for a canvas with just one pixel.
If the new color is the same as the original color, we should return the canvas unchanged to avoid unnecessary work.
Only pixels connected to the starting pixel should be changed, not all pixels of the same color.
Below is the implementation of the digital paint bucket tool:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101// Recursive DFS Approachfunction floodFill(canvas, startRow, startCol, newColor) { // Get the original color const originalColor = canvas[startRow][startCol]; // If the starting pixel is already the new color, no need to do anything if (originalColor === newColor) { return canvas; } // Call the recursive helper function dfs(canvas, startRow, startCol, originalColor, newColor); return canvas;} function dfs(canvas, row, col, originalColor, newColor) { // Check if the current position is valid if (row < 0 || row >= canvas.length || col < 0 || col >= canvas[0].length) { return; } // Check if the current pixel is the original color if (canvas[row][col] !== originalColor) { return; } // Change the color of the current pixel canvas[row][col] = newColor; // Recursively fill the adjacent pixels dfs(canvas, row + 1, col, originalColor, newColor); // Down dfs(canvas, row - 1, col, originalColor, newColor); // Up dfs(canvas, row, col + 1, originalColor, newColor); // Right dfs(canvas, row, col - 1, originalColor, newColor); // Left} // Iterative BFS Approachfunction floodFillBFS(canvas, startRow, startCol, newColor) { // Get the original color const originalColor = canvas[startRow][startCol]; // If the starting pixel is already the new color, no need to do anything if (originalColor === newColor) { return canvas; } // Initialize a queue with the starting pixel const queue = [[startRow, startCol]]; while (queue.length > 0) { const [row, col] = queue.shift(); // Check if the current position is valid if (row < 0 || row >= canvas.length || col < 0 || col >= canvas[0].length) { continue; } // Check if the current pixel is the original color if (canvas[row][col] !== originalColor) { continue; } // Change the color of the current pixel canvas[row][col] = newColor; // Enqueue the adjacent pixels queue.push([row + 1, col]); // Down queue.push([row - 1, col]); // Up queue.push([row, col + 1]); // Right queue.push([row, col - 1]); // Left } return canvas;} // Test casesconst canvas1 = [ ['R', 'R', 'R', 'R'], ['R', 'B', 'B', 'R'], ['R', 'B', 'B', 'R'], ['R', 'R', 'R', 'R']]; console.log("Original Canvas:");printCanvas(canvas1); console.log("\nAfter Flood Fill (DFS):");const filledCanvas1 = floodFill([...canvas1.map(row => [...row])], 1, 1, 'G');printCanvas(filledCanvas1); console.log("\nAfter Flood Fill (BFS):");const filledCanvas2 = floodFillBFS([...canvas1.map(row => [...row])], 1, 1, 'G');printCanvas(filledCanvas2); // Helper function to print the canvasfunction printCanvas(canvas) { for (const row of canvas) { console.log(row.join(' ')); }}
Let's break down the implementation:
Implement the digital paint bucket tool solution in different programming languages.
Below is the implementation of the digital paint bucket tool in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101// Recursive DFS Approachfunction floodFill(canvas, startRow, startCol, newColor) { // Get the original color const originalColor = canvas[startRow][startCol]; // If the starting pixel is already the new color, no need to do anything if (originalColor === newColor) { return canvas; } // Call the recursive helper function dfs(canvas, startRow, startCol, originalColor, newColor); return canvas;} function dfs(canvas, row, col, originalColor, newColor) { // Check if the current position is valid if (row < 0 || row >= canvas.length || col < 0 || col >= canvas[0].length) { return; } // Check if the current pixel is the original color if (canvas[row][col] !== originalColor) { return; } // Change the color of the current pixel canvas[row][col] = newColor; // Recursively fill the adjacent pixels dfs(canvas, row + 1, col, originalColor, newColor); // Down dfs(canvas, row - 1, col, originalColor, newColor); // Up dfs(canvas, row, col + 1, originalColor, newColor); // Right dfs(canvas, row, col - 1, originalColor, newColor); // Left} // Iterative BFS Approachfunction floodFillBFS(canvas, startRow, startCol, newColor) { // Get the original color const originalColor = canvas[startRow][startCol]; // If the starting pixel is already the new color, no need to do anything if (originalColor === newColor) { return canvas; } // Initialize a queue with the starting pixel const queue = [[startRow, startCol]]; while (queue.length > 0) { const [row, col] = queue.shift(); // Check if the current position is valid if (row < 0 || row >= canvas.length || col < 0 || col >= canvas[0].length) { continue; } // Check if the current pixel is the original color if (canvas[row][col] !== originalColor) { continue; } // Change the color of the current pixel canvas[row][col] = newColor; // Enqueue the adjacent pixels queue.push([row + 1, col]); // Down queue.push([row - 1, col]); // Up queue.push([row, col + 1]); // Right queue.push([row, col - 1]); // Left } return canvas;} // Test casesconst canvas1 = [ ['R', 'R', 'R', 'R'], ['R', 'B', 'B', 'R'], ['R', 'B', 'B', 'R'], ['R', 'R', 'R', 'R']]; console.log("Original Canvas:");printCanvas(canvas1); console.log("\nAfter Flood Fill (DFS):");const filledCanvas1 = floodFill([...canvas1.map(row => [...row])], 1, 1, 'G');printCanvas(filledCanvas1); console.log("\nAfter Flood Fill (BFS):");const filledCanvas2 = floodFillBFS([...canvas1.map(row => [...row])], 1, 1, 'G');printCanvas(filledCanvas2); // Helper function to print the canvasfunction printCanvas(canvas) { for (const row of canvas) { console.log(row.join(' ')); }}
The flood fill algorithm changes the color of a pixel and all adjacent pixels of the same color, continuing until no more connected pixels of the original color remain.
Recognize that this is a classic DFS problem where we recursively explore all adjacent pixels of the same color.
The base cases are when the current position is out of bounds, or the current pixel is not the original color, or it's already the new color.
Write a function that handles the base cases and recursively explores the adjacent pixels, changing their colors as we go.
Implement an iterative BFS solution using a queue, which can be more efficient for very large canvases.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the digital paint bucket tool problem using a different approach than shown above.
The algorithm should work correctly for a canvas with just one pixel.
If the new color is the same as the original color, we should return the canvas unchanged to avoid unnecessary work.
Only pixels connected to the starting pixel should be changed, not all pixels of the same color.