Approach
Depth-first search
For Number of Valid Move Combinations On Chessboard, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 92 lines of Java from the credited upstream file 2056.java.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
- 7 loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int countCombinations(String[] pieces, int[][] positions) {3 final int n = pieces.length;4 Set<Long> hashedBoards = new HashSet<>();5 6 7 8 9 List<List<int[]>> combMoves = new ArrayList<>();10 11 getCombMoves(pieces, 0, new ArrayList<>(), combMoves);12 13 for (List<int[]> combMove : combMoves)14 dfs(positions, n, combMove, (1 << n) - 1, hashedBoards);15 16 return hashedBoards.size();17 }18 19 private static Map<String, int[][]> MOVES = new HashMap<>();20 21 static {22 MOVES.put("rook", new int[][] {{1, 0}, {-1, 0}, {0, 1}, {0, -1}});23 MOVES.put("bishop", new int[][] {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}});24 MOVES.put("queen",25 new int[][] {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}});26 }27 28 29 private void getCombMoves(String[] pieces, int ithPiece, List<int[]> path,30 List<List<int[]>> combMoves) {31 if (ithPiece == pieces.length) {32 combMoves.add(new ArrayList<>(path));33 return;34 }35 36 for (int[] move : MOVES.get(pieces[ithPiece])) {37 path.add(move);38 getCombMoves(pieces, ithPiece + 1, path, combMoves);39 path.remove(path.size() - 1);40 }41 }42 43 44 private void dfs(int[][] board, int n, List<int[]> combMove, int activeMask,45 Set<Long> hashedBoards) {46 if (activeMask == 0)47 return;48 hashedBoards.add(getHash(board));49 50 for (int nextActiveMask = 1; nextActiveMask < 1 << n; ++nextActiveMask) {51 if ((activeMask & nextActiveMask) != nextActiveMask)52 continue;53 54 55 int[][] nextBoard = new int[n][];56 for (int i = 0; i < n; ++i)57 nextBoard[i] = board[i].clone();58 59 60 for (int i = 0; i < n; ++i)61 if ((nextActiveMask >> i & 1) == 1) {62 nextBoard[i][0] += combMove.get(i)[0];63 nextBoard[i][1] += combMove.get(i)[1];64 }65 66 67 if (getUniqueSize(nextBoard) < n)68 continue;69 70 71 if (Arrays.stream(nextBoard).allMatch(p -> 1 <= p[0] && p[0] <= 8 && 1 <= p[1] && p[1] <= 8))72 dfs(nextBoard, n, combMove, nextActiveMask, hashedBoards);73 }74 }75 76 77 private long getHash(int[][] board) {78 long hash = 0;79 for (int[] pos : board)80 hash = (hash * 64) + (pos[0] - 1 << 3) + (pos[1] - 1);81 return hash;82 }83 84 85 private int getUniqueSize(int[][] board) {86 Set<Integer> unique = new HashSet<>();87 for (int[] pos : board)88 unique.add(pos[0] * 8 + pos[1]);89 return unique.size();90 }91}92