判断周围8个方向的位置

判断周围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++;
                  }
             }
          }
     }
}
posted @ 2023-10-26 23:04  爱新觉罗LQ  阅读(20)  评论(0)    收藏  举报