Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2056: Number of Valid Move Combinations On Chessboard

In this guide, we solve Leetcode #2056 Number of Valid Move Combinations On Chessboard in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

There is an 8 x 8 chessboard containing n pieces (rooks, queens, or bishops). You are given a string array pieces of length n, where pieces[i] describes the type (rook, queen, or bishop) of the ith piece.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Array, String, Backtracking, Simulation

Intuition

We must explore combinations of choices, but many branches can be pruned early.

Backtracking enumerates valid candidates while keeping the search space under control.

Approach

Use DFS to build candidates step by step, and backtrack when constraints are violated.

Pruning keeps the exploration practical for typical constraints.

Steps:

  • Define the decision tree.
  • DFS through choices and backtrack.
  • Prune invalid paths early.

Example

Input: pieces = ["rook"], positions = [[1,1]] Output: 15 Explanation: The image above shows the possible squares the piece can move to.

Python Solution

rook_dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)] bishop_dirs = [(1, 1), (1, -1), (-1, 1), (-1, -1)] queue_dirs = rook_dirs + bishop_dirs def get_dirs(piece: str) -> List[Tuple[int, int]]: match piece[0]: case "r": return rook_dirs case "b": return bishop_dirs case _: return queue_dirs class Solution: def countCombinations(self, pieces: List[str], positions: List[List[int]]) -> int: def check_stop(i: int, x: int, y: int, t: int) -> bool: return all(dist[j][x][y] < t for j in range(i)) def check_pass(i: int, x: int, y: int, t: int) -> bool: for j in range(i): if dist[j][x][y] == t: return False if end[j][0] == x and end[j][1] == y and end[j][2] <= t: return False return True def dfs(i: int) -> None: if i >= n: nonlocal ans ans += 1 return x, y = positions[i] dist[i][:] = [[-1] * m for _ in range(m)] dist[i][x][y] = 0 end[i] = (x, y, 0) if check_stop(i, x, y, 0): dfs(i + 1) dirs = get_dirs(pieces[i]) for dx, dy in dirs: dist[i][:] = [[-1] * m for _ in range(m)] dist[i][x][y] = 0 nx, ny, nt = x + dx, y + dy, 1 while 1 <= nx < m and 1 <= ny < m and check_pass(i, nx, ny, nt): dist[i][nx][ny] = nt end[i] = (nx, ny, nt) if check_stop(i, nx, ny, nt): dfs(i + 1) nx += dx ny += dy nt += 1 n = len(pieces) m = 9 dist = [[[-1] * m for _ in range(m)] for _ in range(n)] end = [(0, 0, 0) for _ in range(n)] ans = 0 dfs(0) return ans

Complexity

The time complexity is O((n×M)n)O((n \times M)^n)O((n×M)n), and the space complexity is O(n×M)O(n \times M)O(n×M). The space complexity is O(n×M)O(n \times M)O(n×M).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy