package com.company; /* * Paint Fill: Implement the "paint fill" function that one might see on many image editing programs. * That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color, * fill in the surrounding area until the color changes from the original color. * */ public class Main { public static void main(String[] args) { // write your code here int r = 1, c = 1; Color[][] screen = new Color[3][3]; screen[0][0] = Color.black; screen[0][1] = Color.black; screen[0][2] = Color.black; screen[1][0] = Color.black; screen[1][1] = Color.red; screen[1][2] = Color.red; screen[2][0] = Color.red; screen[2][1] = Color.red; screen[2][2] = Color.red; Color newColor = Color.white; boolean isChanged = paintFill(screen, r, c, newColor); System.out.println(isChanged); for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { System.out.print(screen[i][j]); } System.out.println(""); } } public static boolean paintFill(Color[][] screen, int r, int c, Color newColor) { if(screen[r][c] == newColor) { return false; } return paintFill(screen, r, c, screen[r][c], newColor); } public static boolean paintFill(Color[][] screen, int r, int c, Color oldColor, Color newColor) { if(r < 0 || r >= screen.length || c < 0 || c >= screen[0].length) { return false; } if(screen[r][c] != oldColor){ return false; } screen[r][c] = newColor; paintFill(screen, r - 1, c, oldColor, newColor); paintFill(screen, r + 1, c, oldColor, newColor); paintFill(screen, r, c - 1, oldColor, newColor); paintFill(screen, r, c + 1, oldColor, newColor); return true; } }
package com.company; enum Color { black, white, red, green, yellow }