1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution {
static int[][] dirs = new int[][]{{-1, 0}, {0, -1}, {1, 0}, {0, 1}, {1, -1}, {-1, 1}, {-1, -1}, {1, 1}};
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) { int x = king[0], y = king[1]; List<List<Integer>> ans = new ArrayList<>(); boolean[][] visited = new boolean[8][8]; for (int[] queen : queens) { visited[queen[0]][queen[1]] = true; } boolean[] checked = new boolean[8]; for (int step = 1; step <= 8; ++step) { for (int i = 0; i < 8; ++i) { if (checked[i]) continue; int nx = x + dirs[i][0] * step, ny = y + dirs[i][1] * step; if (nx >= 0 && nx < 8 && ny >= 0 && ny < 8) { if (!visited[nx][ny]) continue; ans.add(List.of(nx, ny)); } checked[i] = true; } } return ans; } }
|