2013-03-20 36 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
  • 您对对角线的邻居进行了绑定检查,但(大概)递归调用只会更改一个坐标。