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