gpt4 book ai didi

java - Java 中的递归 Floodfill

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:24:03 29 4
gpt4 key购买 nike

我的任务是使用递归和 2D Ascii 图像在 Java 中编写洪水填充算法。我编写了代码并且它运行良好,但我不确定我是否可以将它写得更简单,因为我使用了太多 if 语句来检查一些东西,比如当前点在哪里(边缘、角或中间)。

代码如下:

public class AsciiShop {

public static void main(String[] args) {
String[] img = new String[5];

img[0] = "++++++#";
img[1] = "+#+++##";
img[2] = "###++#+";
img[3] = "+#++#++";
img[4] = "+####++";


fill(img, 1, 2, '0');

}

public static void fill(String[] image, int x, int y, char c) {

String[] finalImage = image;

char startChar = finalImage[y].charAt(x);

StringBuilder stringBuilder = new StringBuilder(finalImage[y]);
stringBuilder.setCharAt(x, c);

finalImage[y] = stringBuilder.toString();

if(y>0&&x>0&&y<(finalImage.length-1)&&x<(finalImage[y].length()-1)) {
//Linker Nachbar
if(finalImage[y].charAt(x-1) == startChar)
fill(finalImage, x-1, y, c);

//Nachbar oben
if(finalImage[y-1].charAt(x) == startChar)
fill(finalImage, x, y-1, c);

//Rechter Nachbar
if(finalImage[y].charAt(x+1) == startChar)
fill(finalImage, x+1, y, c);

//Nachbar unter
if(finalImage[y+1].charAt(x) == startChar)
fill(finalImage, x, y+1, c);
}
else if(y==0&&x==0) {

//Rechter Nachbar
if(finalImage[y].charAt(x+1) == startChar)
fill(finalImage, x+1, y, c);

//Nachbar unter
if(finalImage[y+1].charAt(x) == startChar)
fill(finalImage, x, y+1, c);

}

else if (y==0&&x==(finalImage[y].length()-1)) {
//Linker Nachbar
if(finalImage[y].charAt(x-1) == startChar)
fill(finalImage, x-1, y, c);

//Nachbar unter
if(finalImage[y+1].charAt(x) == startChar)
fill(finalImage, x, y+1, c);
}

else if (y==finalImage.length&&x==(finalImage[y].length()-1)) {

//Linker Nachbar
if(finalImage[y].charAt(x-1) == startChar)
fill(finalImage, x-1, y, c);

//Nachbar oben
if(finalImage[y-1].charAt(x) == startChar)
fill(finalImage, x, y-1, c);

}

else if (y==finalImage.length&&x==0) {

//Nachbar oben
if(finalImage[y-1].charAt(x) == startChar)
fill(finalImage, x, y-1, c);

//Rechter Nachbar
if(finalImage[y].charAt(x+1) == startChar)
fill(finalImage, x+1, y, c);

}

else if (y==0&&x>0&&x<(finalImage[y].length()-1)){

//Linker Nachbar
if(finalImage[y].charAt(x-1) == startChar)
fill(finalImage, x-1, y, c);

//Rechter Nachbar
if(finalImage[y].charAt(x+1) == startChar)
fill(finalImage, x+1, y, c);

//Nachbar unter
if(finalImage[y+1].charAt(x) == startChar)
fill(finalImage, x, y+1, c);
}

else if (y==(finalImage.length-1)&&x>0&&x<(finalImage[y].length()-1)){
//Linker Nachbar
if(finalImage[y].charAt(x-1) == startChar)
fill(finalImage, x-1, y, c);

//Nachbar oben
if(finalImage[y-1].charAt(x) == startChar)
fill(finalImage, x, y-1, c);

//Rechter Nachbar
if(finalImage[y].charAt(x+1) == startChar)
fill(finalImage, x+1, y, c);
}

else if (x==0&&y>0&&y<(finalImage.length-1)) {


//Nachbar oben
if(finalImage[y-1].charAt(x) == startChar)
fill(finalImage, x, y-1, c);

//Rechter Nachbar
if(finalImage[y].charAt(x+1) == startChar)
fill(finalImage, x+1, y, c);

//Nachbar unter
if(finalImage[y+1].charAt(x) == startChar)
fill(finalImage, x, y+1, c);
}
else if (x==(finalImage[y].length()-1)&&y>0&&y<(finalImage.length-1)) {

//Linker Nachbar
if(finalImage[y].charAt(x-1) == startChar)
fill(finalImage, x-1, y, c);

//Nachbar oben
if(finalImage[y-1].charAt(x) == startChar)
fill(finalImage, x, y-1, c);

//Nachbar unter
if(finalImage[y+1].charAt(x) == startChar)
fill(finalImage, x, y+1, c);
}

for (int i=0; i<finalImage.length; i++) {
System.out.println(finalImage[i]);
}

System.out.println();

}
}

最佳答案

为什么不结合边界检查和“边界/已填充”检查并将它们移动到自己的方法中?

public class ImageFiller {
protected String[] finalImage; // init these in constructor
protected int sizeX;
protected int sizeY;
protected char startChar;
protected char fillChar;


public void fill (int x, int y) {
// ...
visitNeighbor( x-1, y);
visitNeighbor( x+1, y);
visitNeighbor( x, y-1);
visitNeighbor( x, y+1);
// ...
}

protected void visitNeighbor (int x, int y) {
if (x < 0 || x >= sizeX)
return;
if (y < 0 || y >= sizeY)
return;
if (finalImage[y].charAt(x) != startChar) {
// Boundary or Already Filled.
return;
}
// Recursive Fill;
// -- or could use a queue, to avoid excessively deep recursion.
fill( x, y);
}
}

关于java - Java 中的递归 Floodfill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19737614/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com