2014-11-21 51 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

相关问题