fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. private char[][] pixels;
  12.  
  13. public Ideone(char[][] pixels) {
  14. this.pixels = pixels;
  15. }
  16.  
  17. public void fill(int x, int y, char newColor, char oldColor) {
  18. if (x < 0) return;
  19. if (y < 0) return;
  20. if (x >= pixels.length) return;
  21. if (y >= pixels[x].length) return;
  22.  
  23. if (oldColor != pixels[x][y]) return;
  24.  
  25. pixels[x][y] = newColor;
  26.  
  27. fill(x - 1, y, newColor, oldColor);
  28. fill(x + 1, y, newColor, oldColor);
  29. fill(x, y - 1, newColor, oldColor);
  30. fill(x, y + 1, newColor, oldColor);
  31. }
  32.  
  33. public void inspect() {
  34. for (int y = 0; y < pixels.length; y++) {
  35. for (int x = 0; x < pixels[y].length; x++) {
  36. System.out.print(pixels[y][x]);
  37. }
  38. System.out.print("\n");
  39. }
  40. }
  41.  
  42. public static void main(String argv[]) {
  43. char pixels[][] =
  44. {
  45. { 'O', 'X', 'X', 'X', 'X' },
  46. { 'X', 'O', 'O', 'O', 'X' },
  47. { 'X', 'O', '#', 'O', 'X' },
  48. { 'X', 'O', 'O', 'O', 'X' },
  49. { 'X', 'X', 'X', 'X', 'X' },
  50. { 'X', 'X', 'X', '#', '#' },
  51. { 'X', 'X', 'X', 'X', 'X' }
  52. };
  53. Ideone bucketFill = new Ideone(pixels);
  54. bucketFill.fill(0, 0, '*', 'O');
  55. bucketFill.fill(3, 0, 'O', 'O');
  56. bucketFill.fill(2, 1, '@', 'O');
  57. bucketFill.inspect();
  58. }
  59. }
Success #stdin #stdout 0.07s 381248KB
stdin
Standard input is empty
stdout
*XXXX
X@@@X
X@#@X
X@@@X
XXXXX
XXX##
XXXXX