2014-11-21 40 views
2

如果我有一個名爲myArray的2D int數組,例如3x3大小,其元素只能有1或0值,那麼計算特定元素周圍有多少個1的有效方法是什麼? E.g:有效的方法來找到有多少個相鄰元素與一個特定的值有一個特定的元素在一個數組中

[0][0][0] 
[0][1][0] 
[1][1][1] 

元件myArray的[0] [0]將具有1 neighbourCount而myArray的[0] [1]將具有3.

一個neighbourCount這是我的當前蠻力代碼。 myArray的= currentGeneration

public int neighbours(int x, int y) { //x and y are 0 index based coordinates, they are swapped inside to corespond with actual x and y coordinates 
     int neighbourCounter = 0; 
     if(x == 0 && y == 0) { 
      if(currentGeneration[y+1][x] == 1) { 
       neighbourCounter++; 
      } 
      if(currentGeneration[y][x+1] == 1) { 
       neighbourCounter++; 
      } 
      if(currentGeneration[y+1][x+1] == 1) { 
       neighbourCounter++; 
      } 
     } else if(x == 0 && y == currentGeneration.length - 1) { 
          if(currentGeneration[y-1][x] == 1) { 
            neighbourCounter++; 
          } 
          if(currentGeneration[y][x+1] == 1) { 
            neighbourCounter++; 
          } 
          if(currentGeneration[y-1][x+1] == 1) { 
            neighbourCounter++; 
          } 
     } else if(x == currentGeneration[0].length - 1 && y == currentGeneration.length - 1) { 
          if(currentGeneration[y-1][x] == 1) { 
            neighbourCounter++; 
          } 
          if(currentGeneration[y][x-1] == 1) { 
            neighbourCounter++; 
          } 
          if(currentGeneration[y-1][x-1] == 1) { 
            neighbourCounter++; 
          } 
     } else if(y == 0 && x == currentGeneration[0].length - 1) { 
          if(currentGeneration[y][x-1] == 1) { 
            neighbourCounter++; 
          } 
          if(currentGeneration[y+1][x] == 1) { 
            neighbourCounter++; 
          } 
          if(currentGeneration[y+1][x-1] == 1) { 
            neighbourCounter++; 
          } 
     } else if(y == 0) { 
      for(int i = -1; i <= 1; i+=2) { 
       if(currentGeneration[y][x+i] == 1) { 
        neighbourCounter++; 
       } 
      } 
      if(currentGeneration[y+1][x] == 1) { 
       neighbourCounter++; 
      } 
      for(int i = -1; i <= 1; i+=2) { 
       if(currentGeneration[y+1][x+i] == 1) { 
        neighbourCounter++; 
       } 
      } 
     } else if(x == 0) { 
      for(int i = -1; i <= 1; i+=2) { 
       if(currentGeneration[y+i][x] == 1) { 
        neighbourCounter++; 
       } 
      } 
      if(currentGeneration[y][x+1] == 1) { 
       neighbourCounter++; 
      } 
      for(int i = -1; i <= 1; i+=2) { 
       if(currentGeneration[y+i][x+1] == 1) { 
        neighbourCounter++; 
       } 
      } 
     } else if(y == currentGeneration.length - 1) { 
      for(int i = -1; i <= 1; i+=2) { 
       if(currentGeneration[y][x+i] == 1) { 
        neighbourCounter++; 
       } 
      } 
      if(currentGeneration[y-1][x] == 1) { 
       neighbourCounter++; 
      } 
      for(int i = -1; i <= 1; i+=2) { 
       if(currentGeneration[y-1][x+i] == 1) { 
        neighbourCounter++; 
       } 
      } 
     } else if(x == currentGeneration[0].length - 1) { 
      for(int i = -1; i <= 2; i+=2) { 
       if(currentGeneration[y+i][x] == 1) { 
        neighbourCounter++; 
       } 
      } 
      if(currentGeneration[y][x-1] == 1) { 
       neighbourCounter++; 
      } 
      for(int i = -1; i <= 2; i+=2) { 
       if(currentGeneration[y+i][x-1] == 1) { 
        neighbourCounter++; 
       } 
      } 
     } else { 
      for(int i = -1; i <= 1; i+=2) { 
       if(currentGeneration[y+i][x] == 1) { 
        neighbourCounter++; 
       } 
       if(currentGeneration[y][x+i] == 1) { 
        neighbourCounter++; 
       } 
       if(currentGeneration[y+i][x+i] == 1) { 
        neighbourCounter++; 
       } 
       if(currentGeneration[y+i][x-i] == 1) { 
        neighbourCounter++; 
       } 
      } 
     } 
     return neighbourCounter; 
    } 
+1

我猜這是功課? – 2014-11-21 01:28:40

+0

你到目前爲止嘗試過什麼。如果您發佈了一些代碼,我們可以指導您確定您是否做得正確或者是否有更好的方法。 – Maxqueue 2014-11-21 01:32:39

+1

不,我只是想知道是否會有一個有效的方法來做到這一點。我目前的方法涉及通過檢查特定的「中心」元素(其中一個角,其中一個邊或中間)以及然後檢查可能的鄰居而不超出數組邊界來強制執行它。 – echoeida 2014-11-21 01:32:46

回答

0

怎麼樣:

public static int findNeighbors(int x, int y, int[][] a) { 
    int sum = 0; 
    for (int i = (y>0 ? y-1 : 0); i <= (y<a.length-1 ? y+1 : a.length-1); ++i) 
     for (int j = (x>0 ? x-1 : 0); j <= (x<a[0].length-1 ? x+1 : a[0].length-1); ++j) 
      sum += a[i][j]; 
    sum -= a[y][x]; 
    return sum; 
} 
+0

請注意第6行的編輯:'sum - = a [y] [x];'而不是'sum - = a [x] [y];'指責笛卡爾座標的順序不同於數組索引:) – 2014-11-21 02:45:07

相關問題