Finding neighbours in a two-dimensional array

后端 未结 20 1122
忘了有多久
忘了有多久 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:25
    private ArrayList<Element> getNeighbors(Element p) {
        ArrayList<Element> n = new ArrayList<Element>();
    
        for (int dr = -1; dr <= +1; dr++) {
            for (int dc = -1; dc <= +1; dc++) {
                int r = p.row + dr;
                int c = p.col + dc;
    
                if ((r >= 0) && (r < ROWS) && (c >= 0) && (c < COLS)) {
                    // skip p
                    if ((dr != 0) || (dc != 0))
                        n.add(new Element(r, c));
                }               
            }
        }
    
        return n;
    }
    
    0 讨论(0)
  • 2020-11-30 02:27

    Grid (vector 2D or one dimension... not the problem here)
    X & Y, coordinate of your element (or just pass your vector element by ref...)

    int neighbour(const Grid & g, const size_t & x, const size_t & y) {
        for (int i = -1; i < 2; ++i)
            for (int j = -1; j < 2; ++j)
                if (x + i >= 0 && x + i < g.row && y + j >= 0 && y + j < g.col)
                    //Do some stuff
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-30 02:29

    JS sample :

        function findingNeighbors(myArray, i, j){
            return myArray.reduce(function(a, b, c){
                if(Math.max(0, i-1) <= c && c <= Math.min(i+1, myArray.length-1)){
                    a = a.concat(
                        b.reduce(function(d, e, f){
                        if(f == j && c == i)
                            return d;
                        if(Math.max(0, j-1) <= f && f <= Math.min(j+1, myArray.length-1))
                            d.push(e)
                        return d;
                        },[])
                    );
                }
                return a;
            },[]);
        }
    
    0 讨论(0)
  • 2020-11-30 02:33

    Rows and Cols are total number of rows and cols

    Define a CellIndex struct or class. Or you can just return the actual values instead of the indexes.

    public List<CellIndex> GetNeighbors(int rowIndex, int colIndex)
    {
    var rowIndexes = (new int[] { rowIndex - 1, rowIndex, rowIndex + 1 }).Where(n => n >= 0 && n < Rows);
    
    var colIndexes = (new int[] { colIndex - 1, colIndex, colIndex + 1 }).Where(n => n >= 0 && n < Cols);
    
    return (from row in rowIndexes from col in colIndexes where row != rowIndex || col != colIndex select new CellIndex { Row = row, Col = col }).ToList();
    }
    
    0 讨论(0)
  • 2020-11-30 02:35

    Here is a convenient method in Python:

    def neighbors(array,pos):
        n = []
         string = "array[pos.y+%s][pos.x+%s]"
        for i in range(-1,2):
            for j in range(-1,2):
                n.append(eval(string % (i,j)))
        return n
    

    Assuming pos is some 2D Point object and array is a 2D array.

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

    (pseudo-code)

    row_limit = count(array);
    if(row_limit > 0){
      column_limit = count(array[0]);
      for(x = max(0, i-1); x <= min(i+1, row_limit); x++){
        for(y = max(0, j-1); y <= min(j+1, column_limit); y++){
          if(x != i || y != j){
            print array[x][y];
          }
        }
      }
    }
    

    Of course, that takes almost as many lines as the original hard-coded solution, but with this one you can extend the "neighborhood" as much as you can (2-3 or more cells away)

    0 讨论(0)
提交回复
热议问题