Finding neighbours in a two-dimensional array

后端 未结 20 1102
忘了有多久
忘了有多久 2020-11-30 02:02

Is there an easy way of finding the neighbours (that is, the eight elements around an element) of an element in a two-dimensional array? Short of just subtracting and adding

相关标签:
20条回答
  • 2020-11-30 02:37

    array[i][j] has neighbors

    array[i-1][j]
    array[i][j-1]
    array[i-1][j-1]
    array[i+1][j]
    array[i][j+1]
    array[i+1][j+1]
    array[i+1][j-1]
    array[i-1][j+1]
    

    That's probably the fastest/easiest way is to just list all possible neighbors. Make sure to do index out of bound checking though.

    Some languages might offer a shortcut way of doing this, but I don't know of any.

    0 讨论(0)
  • 2020-11-30 02:38

    Since in a matrix around an element there are only 8 elements, you can use array to store different index values.For e.g.,

      int iarr[8] = {-1,-1,-1,0,0,+1,+1,+1};
      int jarr[8] = {-1,0,+1,-1,+1,-1,0,+1};
      for(int i = 0 ; i < 8 ; i++)
       {
        if(arr[x-iarr[i]][y-jarr[i]] == 1)
        {
         //statements
        }
       }  
      /* x and y are the position of elements from where you want to reach out its neighbour */
    

    since both array contains just 8 values , then space might not be a problem.

    0 讨论(0)
  • 2020-11-30 02:40

    This is an implementation of @Seb's answer in python3+ that is concise and uses generators for max performance:

    def neighbours(pos, matrix):
        rows = len(matrix)
        cols = len(matrix[0]) if rows else 0
        for i in range(max(0, pos[0] - 1), min(rows, pos[0] + 2)):
            for j in range(max(0, pos[1] - 1), min(cols, pos[1] + 2)):
                if (i, j) != pos:
                    yield matrix[i][j]
    
    0 讨论(0)
  • 2020-11-30 02:40

    A lot depends on what your data is. For example, if your 2D array is a logical matrix, you could convert rows to integers and use bitwise operations to find the ones you want.

    For a more general-purpose solution I think you're stuck with indexing, like SebaGR's solution.

    0 讨论(0)
  • 2020-11-30 02:42

    The approach I usually take is described on the bottom of this blog: https://royvanrijn.com/blog/2019/01/longest-path/

    Instead of hardcoding the directions or having two nested loops I like to use a single integer loop for the 8 ‘directions’ and use (i % 3)-1 and (i / 3)-1; do check out the blog with images.

    It doesn’t nest as deep and is easily written, not a lot of code needed!

    0 讨论(0)
  • 2020-11-30 02:43

    Here is a working Javascript example from @seb original pseudo code:

    function findingNeighbors(myArray, i, j) {
      var rowLimit = myArray.length-1;
      var columnLimit = myArray[0].length-1;
    
      for(var x = Math.max(0, i-1); x <= Math.min(i+1, rowLimit); x++) {
        for(var y = Math.max(0, j-1); y <= Math.min(j+1, columnLimit); y++) {
          if(x !== i || y !== j) {
            console.log(myArray[x][y]);
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题