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
| class Solution { private static final int[][] DIRECTIONS = new int[][]{{-1, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) { List<List<Integer>> resultList = new ArrayList<>();
boolean[][] queenBoard = new boolean[8][8]; for (int[] queen : queens) { queenBoard[queen[0]][queen[1]] = true; }
for (int[] direction : DIRECTIONS) { int i = king[0] + direction[0], j = king[1] + direction[1]; while (i >= 0 && i < 8 && j >= 0 && j < 8) { if (queenBoard[i][j]) { resultList.add(Arrays.asList(i, j)); break; } i += direction[0]; j += direction[1]; } }
return resultList; } }
|