Java 用泛光填充填充ASCII图像

Java 用泛光填充填充ASCII图像,java,recursion,flood-fill,Java,Recursion,Flood Fill,嗨,我想用泛光填充alg填充ASCII图像。我总是收到错误信息,我不知道为什么 以下是我处理输入图像的主要方法: public static void main(String[] args){ Scanner sc = new Scanner("read 12\n+++++++++++++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n+++++

嗨,我想用泛光填充alg填充ASCII图像。我总是收到错误信息,我不知道为什么

以下是我处理输入图像的主要方法:

    public static void main(String[] args){
    Scanner sc = new Scanner("read 12\n+++++++++++++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#++\n++++++++++#--\nfill 2 2 d");
    String read, numberst;
    String [] image;
    int number = 0, count = 0;
    Boolean error = false;

    read = sc.next();
    numberst = sc.next();

    if ((read + numberst).matches("read[0-9]+")) {
        number = Integer.parseInt(numberst);
    } else {
        error = true;
    }

    image = new String[number];

    System.out.println("image");

    while (sc.hasNextLine() && (error == false) && (count <= number - 1)) {
        image[count] = sc.next();
        //System.out.println(image[count] + " " + count);
        count++;
        if (image[0].length() != image[count - 1].length())  {
            error = true;
            System.out.println("INPUT MISMATCH");
        }
    }

    System.out.println("fill");
    while(sc.hasNextLine() && (error == false) && sc.hasNext()) {
        String fill = "", xstr = "", ystr = "", cstr;

        fill = sc.next();

        if (sc.hasNext()) {
            xstr = sc.next();   
        } else {
            error = true;
            System.out.println("INPUT MISMATCH");
        }

        if (sc.hasNext() && error == false){
            ystr = sc.next();
        } else {
            error = true;
            System.out.println("INPUT MISMATCH");
        }

        if (sc.hasNext() && error == false){
            cstr = sc.next();

            if ((fill + xstr + ystr + cstr).matches("fill[0-9]+[0-9]+.")) {
            int x = Integer.parseInt(xstr), y = Integer.parseInt(ystr);
            char c = cstr.charAt(0);

            if (x <= image[0].length() && y <= number) {
                fill(image, x, y, c);
            } else {
                error = true;
                System.out.println("OPERATION FAILED");
            }
            //System.out.println(fill + x + y + c);
        } else {
            error = true;
            System.out.println("INPUT MISMATCH");
        }

        } else {
            error = true;
            System.out.println("INPUT MISMATCH");
        }
        //System.out.println(error);
    }

    if (error == false) {
        for (int i = 0; i < number; ++i) {
        System.out.println(image[i]);
        }
    }
}
我不知道为什么这不起作用。
我希望你能帮助我。

首先,你在填水时做错了:在继续之前,你应该检查一下是否要向正确的方向前进;您首先检查所有方向,然后再继续检查。这会导致已经被替换的处理位置导致无限循环。
在第二个时间,
if((y+1)中有一个输入错误什么症状让你认为它不起作用?请尽可能提供示例数据。诀窍是读取错误消息。它有助于识别错误是什么,以及错误在哪里。我已将错误消息添加到我的帖子中。这不是错误消息中有趣的部分,但它确实建议无限递归。我无法获得更多信息因为我无法滚动到错误消息的开头,所以无法显示我的终端窗口。问题是我不知道这种无限递归是如何发生的。非常感谢。现在它可以工作了!我希望我可以用另一种方式来做,但我们的大学不希望我们这样做。
    public static void fill(String[] image, int x, int y, char c) {
    char old = (image[y]).charAt(x);
    Boolean r, l, o, u;

    image[y] = (image[y]).substring(0, x) + c + (image[y]).substring(x + 1);


    if ((x + 1) < image[0].length()) {
        r = (image[y].charAt(x + 1) == old);
    } else {
        r = false;
    }
    if ((x - 1) >= 0) {
        l = (image[y].charAt(x - 1) == old);
    }else {
        l = false;
    }
    if ((y - 1) >= 0) {
        o = (image[y - 1].charAt(x) == old);
    } else {
        o = false;
    }
    if ((y + 1) <= image.length) {
        u = (image[y + 1].charAt(x) == old);
    } else {
        u = false;
    }


    if (r == true) {
        fill(image, x + 1, y, c); //According to the Error message it looks like the Java Compiler switches between this and...
    }
    if (l == true) {
        fill(image, x - 1, y, c); //this line.
    }
    if (o == true) {
        fill(image, x, y - 1, c);
    }
    if (u == true) {
        fill(image, x, y + 1, c);
    }


}
    at AsciiShop.fill(AsciiShop.java:122)
    at AsciiShop.fill(AsciiShop.java:119)
    at AsciiShop.fill(AsciiShop.java:122)
    at AsciiShop.fill(AsciiShop.java:119)
    at AsciiShop.fill(AsciiShop.java:122)
    at AsciiShop.fill(AsciiShop.java:119)
    at AsciiShop.fill(AsciiShop.java:122)
    at AsciiShop.fill(AsciiShop.java:119)
    at AsciiShop.fill(AsciiShop.java:122)
public static void fill(String[] image, int x, int y, char c) {
    char old = (image[y]).charAt(x);

    image[y] = (image[y]).substring(0, x) + c + (image[y]).substring(x + 1);

    if ((x + 1) < image[0].length() && 
            image[y].charAt(x + 1) == old) {
        fill(image, x + 1, y, c);
    }

    if ((x - 1) >= 0 &&
            image[y].charAt(x - 1) == old) {
        fill(image, x - 1, y, c);
    }

    if ((y - 1) >= 0 &&
            image[y - 1].charAt(x) == old) {
        fill(image, x, y - 1, c);
    }

    if ((y + 1) < image.length &&
            image[y + 1].charAt(x) == old) {
        fill(image, x, y + 1, c);
    }
}
public static void main(String[] args) {
    Scanner sc = new Scanner(
            "read 12\n"
            + "+++++++++++++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#++\n"
            + "++++++++++#--\n"
            + "fill 2 2 d");
    String read, numberst;
    String[] image;
    int number = 0, count = 0;
    Boolean error = false;

    read = sc.next();
    numberst = sc.next();

    if ((read + numberst).matches("read[0-9]+")) {
        number = Integer.parseInt(numberst);
    } else {
        error = true;
    }

    image = new String[number];

    System.out.println("image");

    while (sc.hasNextLine() && (error == false) && (count <= number - 1)) {
        image[count] = sc.next();
        // System.out.println(image[count] + " " + count);
        count++;
        if (image[0].length() != image[count - 1].length()) {
            error = true;
            System.out.println("INPUT MISMATCH");
        }
    }

    System.out.println("fill");
    while (sc.hasNextLine() && (error == false) && sc.hasNext()) {
        String fill = "", xstr = "", ystr = "", cstr;

        fill = sc.next();

        if (sc.hasNext()) {
            xstr = sc.next();
        } else {
            error = true;
            System.out.println("INPUT MISMATCH");
        }

        if (sc.hasNext() && error == false) {
            ystr = sc.next();
        } else {
            error = true;
            System.out.println("INPUT MISMATCH");
        }

        if (sc.hasNext() && error == false) {
            cstr = sc.next();

            if ((fill + xstr + ystr + cstr).matches("fill[0-9]+[0-9]+.")) {
                int x = Integer.parseInt(xstr), y = Integer.parseInt(ystr);
                char c = cstr.charAt(0);

                if (x <= image[0].length() && y <= number) {
                    fill(image, x, y, image[y].charAt(x), c);
                } else {
                    error = true;
                    System.out.println("OPERATION FAILED");
                }
                // System.out.println(fill + x + y + c);
            } else {
                error = true;
                System.out.println("INPUT MISMATCH");
            }

        } else {
            error = true;
            System.out.println("INPUT MISMATCH");
        }
        // System.out.println(error);
    }

    if (error == false) {
        for (int i = 0; i < number; ++i) {
            System.out.println(image[i]);
        }
    }
}

public static void fill(final String[] image, final int x, final int y, final char oldValue, final char newValue) {
    if (oldValue == image[y].charAt(x)) {
        image[y] = image[y].substring(0, x) + newValue + image[y].substring(x + 1);

        if ((x + 1) < image[y].length()) {
            fill(image, x + 1, y, oldValue, newValue);
        }

        if ((x - 1) >= 0) {
            fill(image, x - 1, y, oldValue, newValue);
        }

        if ((y - 1) >= 0) {
            fill(image, x, y - 1, oldValue, newValue);
        }

        if ((y + 1) < image.length) {
            fill(image, x, y + 1, oldValue, newValue);
        }
    }
}