查找数组中特定元素周围有多少具有特定值的相邻元素的有效方法

时间:2014-11-21 01:27:22

标签: java arrays

如果我有一个名为myArray的2D int数组,例如3x3大小且其元素只能有1或0的值,那么计算特定元素周围有多少1的有效方法是什么? E.g:

[0][0][0]
[0][1][0]
[1][1][1]

元素myArray [0] [0]的neighbourCount为1,而myArray [0] [1]的neighbourCount为3。

这是我目前的蛮力代码。 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 个答案:

答案 0 :(得分: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;
}