0
嘿,这工作正常,但经过与4路比较后,我找不到任何区别... 如果我将这个手中,它会被认为是一个实施8路洪水算法的正确方法? 是/否的答案就足够了,但我想继续这是一个洪水填充计划8路?
private void flood8(int col, int row, Color startColour) {
if (startColour.equals(getPixel(col, row))) {
setPixel(col, row);
// this bit makes the animation work by
// drawing intermediate results, and slowing the updates down
synchronized (this) {
draw();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
// now we call the routine recursively for each neighbour
// the "guard" surrounding each call ensures that we do
// not try to keep going past the edge of the raster
if (col + 1 < COLS) {
flood4(col + 1, row, startColour);
System.out.println("Position1 " + col + ", " + row);
}
if (col - 1 >= 0) {
flood4(col - 1, row, startColour);
System.out.println("Position2 " + col + ", " + row);
}
if (row + 1 < ROWS) {
flood4(col, row + 1, startColour);
System.out.println("Position3 " + col + ", " + row);
}
if (row - 1 <= 0) {
flood4(col, row - 1, startColour);
System.out.println("Position4 " + col + ", " + row);
}
if (col + 1 < COLS && row + 1 < ROWS) {
flood4(col + 1, row, startColour);
System.out.println("Position1 " + col + ", " + row);
}
if (col + 1 < COLS && row - 1 >= 0) {
flood4(col - 1, row, startColour);
System.out.println("Position2 " + col + ", " + row);
}
if (row - 1 >= 0 && row + 1 < ROWS) {
flood4(col, row + 1, startColour);
System.out.println("Position3 " + col + ", " + row);
}
if (row - 1 >= 0 && row - 1 >= 0) {
flood4(col, row - 1, startColour);
System.out.println("Position4 " + col + ", " + row);
}
}
}
感谢您阅读
你在这个算法中多次调用'flood4'方法。该方法的定义在哪里? – Kevin 2013-03-20 20:00:58
似乎是你在洪水中调用flood4的地方,这就是为什么你没有看到任何区别。 – 2013-03-20 20:23:49
你应该在函数的顶部进行边界检查,而不是在每次调用之前。这样它就在一个地方,而不是8个。 – 2013-03-20 20:25:13