0

Doing homework, implementation of the algorithm flood fills. I'm writing a program to this guide: http://en.wikipedia.org/wiki/Flood_fill. And I have some questions:

  1. Is it normal to specify the parameters in function replaces the color of any character bucketFill.fill (0, 0, '*', 'O');, I do not know what kind of color will be initially to these coordinates?
  2. An algorithm is correct? I wrote it for example in Wikipedia, but the result of my program is as follows:

@@@@@ @@@@@ @@@@@ @@@@@ @@@@@ @@@@@ @@@@@

I think must be this:

*@@@@
@@@@@
@@#@@
@@@@@
@@@@@
@@@##
@@@@@

My code:

class BucketFill {

    private char[][] pixels;

    public BucketFill(char[][] pixels) {
        this.pixels = pixels;
    }

    public void fill(int x, int y, char newColor, char oldColor) {
        if (x < 0) return;
        if (y < 0) return;
        if (x >= pixels.length) return;
        if (y >= pixels[x].length) return;

        oldColor = pixels[x][y];

        if (newColor == pixels[x][y]) return;
        if (oldColor != pixels[x][y]) return;

        pixels[x][y] = newColor;

        fill(x - 1, y, newColor, oldColor);
        fill(x + 1, y, newColor, oldColor);
        fill(x, y - 1, newColor, oldColor);
        fill(x, y + 1, newColor, oldColor);
    }

    public void inspect() {
        for (int y = 0; y < pixels.length; y++) {
            for (int x = 0; x < pixels[y].length; x++) {
                System.out.print(pixels[y][x]);
            }
            System.out.print("\n");
        }
    }

    public static void main(String argv[]) {
        char pixels[][] =
        {
            { 'O', 'X', 'X', 'X', 'X' },
            { 'X', 'O', 'O', 'O', 'X' },
            { 'X', 'O', '#', 'O', 'X' },
            { 'X', 'O', 'O', 'O', 'X' },
            { 'X', 'X', 'X', 'X', 'X' },
            { 'X', 'X', 'X', '#', '#' },
            { 'X', 'X', 'X', 'X', 'X' }
        };
        BucketFill bucketFill = new BucketFill(pixels);
        bucketFill.fill(0, 0, '*', 'O');
        bucketFill.fill(3, 0, 'O', 'O');
        bucketFill.fill(2, 1, '@', 'O');
        bucketFill.inspect();
    }
}
rel1x
  • 2,351
  • 4
  • 34
  • 62

1 Answers1

0

Your code breaks because of this line:

oldColor = pixels[x][y];

The value of oldColor must stay the same as what you passed in originally. Remove this line to fix your code. You can also remove the next line, because it does not change the logic:

 if (newColor == pixels[x][y]) return;

The result should look like this:

*XXXX
X@@@X
X@#@X
X@@@X
XXXXX
XXX##
XXXXX

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you for answer! I have a more question: but if I can't see current color here: `fill(0, 0, '*', 'O')`? In my example, I use not current color. – rel1x Dec 07 '14 at 14:55
  • @pertpoert When you pass a color that's different from the current color, the result must not be changed. – Sergey Kalinichenko Dec 07 '14 at 15:48
  • 1
    To add to this answer, look at Boundary Fill algorithm too. They are both similar algorithms but the other one might solve the "not-knowing-old-colour" problem. – shebang Dec 07 '14 at 16:18
  • @dasblinkenlight if I change current color, result another :(. – rel1x Dec 08 '14 at 10:25
  • @pertpoert Once that line is removed, your code works as expected (see the demo link at the bottom). – Sergey Kalinichenko Dec 08 '14 at 11:03