2013-03-20 19 views
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);    
     } 
    } 
} 

感謝您閱讀

+0

你在這個算法中多次調用'flood4'方法。該方法的定義在哪裏? – Kevin 2013-03-20 20:00:58

+0

似乎是你在洪水中調用flood4的地方,這就是爲什麼你沒有看到任何區別。 – 2013-03-20 20:23:49

+1

你應該在函數的頂部進行邊界檢查,而不是在每次調用之前。這樣它就在一個地方,而不是8個。 – 2013-03-20 20:25:13

回答

0

翻了註釋到一個答案之前,我想請問專家,得到了這一點的「懸而未決「排隊。社區wiki,請隨時添加。

它會被認爲是一種實現8路洪水算法的正確方法嗎?

大概不會,由於以下原因:

  • 你叫flood4,而正確的遞歸應該再打電話flood8
  • 您對對角線的鄰居進行了綁定檢查,但(大概)遞歸調用只會更改一個座標。