Number of Valid Move Combinations On Chessboard - Problem

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 of the ith piece. In addition, you are given a 2D integer array positions also of length n, where positions[i] = [ri, ci] indicates that the ith piece is currently at the 1-based coordinate (ri, ci) on the chessboard.

When making a move for a piece, you choose a destination square that the piece will travel toward and stop on.

  • A rook can only travel horizontally or vertically from (r, c) to the direction of (r+1, c), (r-1, c), (r, c+1), or (r, c-1).
  • A queen can only travel horizontally, vertically, or diagonally from (r, c) to the direction of (r+1, c), (r-1, c), (r, c+1), (r, c-1), (r+1, c+1), (r+1, c-1), (r-1, c+1), (r-1, c-1).
  • A bishop can only travel diagonally from (r, c) to the direction of (r+1, c+1), (r+1, c-1), (r-1, c+1), (r-1, c-1).

You must make a move for every piece on the board simultaneously. A move combination consists of all the moves performed on all the given pieces. Every second, each piece will instantaneously travel one square towards their destination if they are not already at it. All pieces start traveling at the 0th second. A move combination is invalid if, at a given time, two or more pieces occupy the same square.

Return the number of valid move combinations.

Note: No two pieces will start in the same square. You may choose the square a piece is already on as its destination. If two pieces are directly adjacent to each other, it is valid for them to move past each other and swap positions in one second.

Input & Output

Example 1 — Basic Two Pieces
$ Input: pieces = ["rook","bishop"], positions = [[1,1],[1,8]]
Output: 15
💡 Note: Rook at (1,1) can move horizontally/vertically, bishop at (1,8) can move diagonally. They have multiple valid combinations without collision.
Example 2 — Single Piece
$ Input: pieces = ["queen"], positions = [[1,1]]
Output: 22
💡 Note: A single queen at (1,1) can stay in place or move to any of 21 valid squares on the 8x8 board.
Example 3 — Adjacent Pieces
$ Input: pieces = ["rook","rook"], positions = [[1,1],[1,2]]
Output: 49
💡 Note: Two rooks adjacent horizontally can move independently in many directions without collision.

Constraints

  • 1 ≤ pieces.length ≤ 4
  • pieces[i] is either "rook", "queen", or "bishop"
  • There will be at most one piece of each type on the board
  • 1 ≤ xi, yi ≤ 8
  • All pieces are on distinct squares

Visualization

Tap to expand
Chess Move Combinations ProblemInput BoardRBRook (1,1)Bishop (1,8)Movement SimulationTime Step 0: InitialTime Step 1: MovingTime Step 2: FinalCheck collisionsValid Combinations✓ Rook stays, Bishop moves✓ Both pieces move safely✗ Collision detectedCount valid onlyResult: 15 valid combinationsEach piece can move to multiple destinationswithout colliding during movement🎯 Key Insight: Simulate movement step-by-step to detect collisions
Understanding the Visualization
1
Input
Chess pieces with their starting positions
2
Process
Generate all valid move combinations with collision checking
3
Output
Count of valid move combinations
Key Takeaway
🎯 Key Insight: Simulate piece movement step-by-step in time to detect collisions during transit, not just at final positions
Asked in
Google 12 Facebook 8 Microsoft 6
15.4K Views
Medium Frequency
~35 min Avg. Time
267 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen