O(queens) Concise Solution for Unbounded Board
Many current solutions exploit the fact of limited 8\xD78 board. However this problem can be easily extended to an unbounded board as a follow-up. In this case,
Many current solutions exploit the fact of limited 8\xD78 board. However this problem can be easily extended to an unbounded board as a follow-up. In this case, most current solutions will be invalidated, while other solutions are much too cumbersome to write.\n\nI\'d like to give a substantially shorter but versatile solution for this case.\n\n```java\nclass Solution {\n    static final int[][] direcs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};\n    \n    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {\n        int[][] memo = new int[8][];\n        for (int[] queen : queens) {\n            int x = queen[0] - king[0];\n            int y = queen[1] - king[1];\n            \n            for (int i = 0; i < 8; ++i) {\n                int[] direc = direcs[i];\n                if (x * direc[1] == y * direc[0] && x * direc[0] >= 0 && y * direc[1] >= 0) {\n                    if (memo[i] == null || distanceOf(king, memo[i]) > distanceOf(king, queen)) {\n                        memo[i] = queen;\n                    }\n                }\n            }\n        }\n        return Arrays.stream(memo)\n            .filter(Objects::nonNull)\n            .map(pair -> List.of(pair[0], pair[1]))\n            .collect(Collectors.toList());\n    }\n    \n    private int distanceOf(int[] p1, int[] p2) {\n        return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);\n    }\n}\n```
[]