More efficient way to check neighbours in a two-dimensional array in Java

前端 未结 7 1778
难免孤独
难免孤独 2020-12-14 11:50

Hey all, for a few of my college assignments I\'ve found the need to check neighbouring cells in 2-dimensional arrays (grids). The solution I\'ve used is a bit of a hack usi

相关标签:
7条回答
  • 2020-12-14 12:32

    why can't you check row+rowMod and col+colMod for validity before array access?

    something like:

     r=row+rowMod;
     c=col+colMod;
     if (r < 0 || c < 0 || r >= grid.length || c >= grid.length) continue;
    

    alternatively (no continue):

     if (r >= 0 && c >= 0 && r < grid.length && c < grid.length && 
         someVar == grid[r][c]) { /* do something */ }
    
    0 讨论(0)
  • 2020-12-14 12:32

    If I understand your code correctly, and am correctly guessing your concerns, you're trying to avoid checking a non-existent neighbour when the cell of interest is on one edge of the grid. One approach, which may or may not suit your application, is to put a 1-cell wide border all the way round your grid. You then run your loops across the interior of this expanded grid, and all the cells you check have 4 neighbours (or 8 if you count the diagonally neighbouring cells).

    0 讨论(0)
  • So row and col currently contain the coordinate of the cell that I want to check the neighbours of. So if I have a class variable called START_OF_GRID which contains 0, my solution would be as follows:

    int rowStart  = Math.max( row - 1, START_OF_GRID   );
    int rowFinish = Math.min( row + 1, grid.length - 1 );
    int colStart  = Math.max( col - 1, START_OF_GRID   );
    int colFinish = Math.min( col + 1, grid.length - 1 );
    
    for ( int curRow = rowStart; curRow <= rowFinish; curRow++ ) {
        for ( int curCol = colStart; curCol <= colFinish; curCol++ ) {
            // do something
        }
    }
    
    0 讨论(0)
  • 2020-12-14 12:46

    You can try this. First decide the size of the grid Lets say its 8 X 8 & assign MIN_X = 0, MIN_Y = 0, MAX_X =7, MAX_Y =7

    Your curren position is represented by thisPosX , thisPosY, then try this:

    int startPosX = (thisPosX - 1 < MIN_X) ? thisPosX : thisPosX-1;
    int startPosY = (thisPosY - 1 < MIN_Y) ? thisPosY : thisPosY-1;
    int endPosX =   (thisPosX + 1 > MAX_X) ? thisPosX : thisPosX+1;
    int endPosY =   (thisPosY + 1 > MAX_Y) ? thisPosY : thisPosY+1;
    
    
    // See how many are alive
    for (int rowNum=startPosX; rowNum<=endPosX; rowNum++) {
        for (int colNum=startPosY; colNum<=endPosY; colNum++) {
            // All the neighbors will be grid[rowNum][colNum]
        }
    }
    

    you can finish it in 2 loops.

    0 讨论(0)
  • 2020-12-14 12:53
    private void fun(char[][] mat, int i, int j){
        int[] ith = { 0, 1, 1, -1, 0, -1 ,-1, 1};
        int[] jth = { 1, 0, 1, 0, -1, -1 ,1,-1};
         // All neighbours of cell
         for (int k = 0; k < 8; k++) {
                if (isValid(i + ith[k], j + jth[k], mat.length)) {
                    //do something here 
                }
            }
    }
    
    private boolean isValid(int i, int j, int l) {
            if (i < 0 || j < 0 || i >= l || j >= l)
                return false;
            return true;
    }
    
    0 讨论(0)
  • 2020-12-14 12:55

    How about this:

    private static void printNeighbours(int row, int col, int[][] Data, int rowLen, int colLen)
    {
        for(int nextR=row-1; nextR<=row+1; nextR++)
        {
            if(nextR<0 || nextR>=rowLen)
                continue;  //row out of bound
            for(int nextC=col-1; nextC<=col+1; nextC++)
            {
                if(nextC<0 || nextC>=colLen)
                    continue;  //col out of bound
                if(nextR==row && nextC==col)
                    continue;    //current cell
                System.out.println(Data[nextR][nextC]);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题