判断周围8个方向的位置
static int[][] offsets = {{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (arr[i][j] == 5){
for (int[] offset : offsets) {
int x = i + offset[0];
int y = j + offset[1];
if (x >= 0 && x < row && y >= 0 && y < col && arr[x][y] == 1){ // 不越界
list.add(new Dir(x, y));
res++;
}
}
}
}
}
标签:arr,判断,offsets,int,位置,周围,++,&&,offset
From: https://www.cnblogs.com/aclq/p/17790733.html