代码之家  ›  专栏  ›  技术社区  ›  Sean Kelleher

在Java中更有效地检查二维数组中的邻居

  •  6
  • Sean Kelleher  · 技术社区  · 14 年前

    if 像我的一些同学一样。我现在的解决方案是

    for ( int row = 0; row < grid.length; row++ ) {
        for ( int col = 0; col < grid.length; col++ ) {
            // this section will usually be in a function
            // checks neighbours of the current "cell"
            try {
                for ( int rowMod = -1; rowMod <= 1; rowMod++ ) {
                    for ( int colMod = -1; colMod <= 1; colMod++ ) {
                        if ( someVar == grid[row+rowMod][col+colMod] ) {
                            // do something
                        }
                    }
                }
            } catch ( ArrayIndexOutOfBoundsException e ) {
                // do nothing, continue
            }
            // end checking neighbours
        }
    }
    

    7 回复  |  直到 14 年前
        1
  •  23
  •   Arturo Herrero Viren    12 年前

    你可以试试这个。

    你现在的位置是由thisPosX,thisPosY表示的,然后试试这个:

    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]
        }
    }
    

        2
  •  6
  •   Sean Kelleher    14 年前

    所以 row col 当前包含要检查其邻居的单元格的坐标。所以如果我有一个类变量 START_OF_GRID 其中包含 0 ,我的解决方案如下:

    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
        }
    }
    
        3
  •  3
  •   DennyRolling    14 年前

    类似于:

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

    或者(没有 ):

     if (r >= 0 && c >= 0 && r < grid.length && c < grid.length && 
         someVar == grid[r][c]) { /* do something */ }
    
        4
  •  1
  •   Mark Elliot    14 年前

    for ( int row = 1; row < grid.length - 1; row++ ) {
        for ( int col = 1; col < grid.length - 1; col++ ) {
            // this section will usually be in a function
            // checks neighbours of the current "cell"
            for ( int rowMod = -1; rowMod <= 1; rowMod++ ) {
                for ( int colMod = -1; colMod <= 1; colMod++ ) {
                    if ( someVar == grid[row+rowMod][col+colMod] ) {
                        // do something
                    }
                }
            }
            // end checking neighbours
        }
    }
    

    与您当前的代码一样,这不一定适合处理边缘条件——也就是说,它在3x3网格适合矩阵的任何地方应用3x3网格,但在矩阵边缘时不会将网格缩小为2x2、2x3或3x2网格。但是,它允许主体中的方法检查3x3网格,以观察矩阵中的每个单元格。

        5
  •  1
  •   High Performance Mark    14 年前

    如果我正确地理解了您的代码,并且正确地猜测了您的关注点,那么当感兴趣的单元位于网格的一个边缘时,您将试图避免检查不存在的邻居。一种可能适合也可能不适合您的应用程序的方法是在网格周围放置一个1个单元格宽的边框。然后,在这个扩展网格的内部运行循环,您检查的所有单元格都有4个相邻单元格(如果计算对角相邻单元格,则有8个相邻单元格)。

        6
  •  1
  •   sumit    10 年前

    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]);
            }
        }
    }
    
        7
  •  0
  •   Vikas Tiwari    7 年前
    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;
    }