Number of Valid Move Combinations On Chessboard - Problem

๐Ÿ Chess Piece Choreography Challenge

Imagine you're directing a synchronized chess performance where multiple pieces must move simultaneously across an 8ร—8 chessboard without colliding! You have n chess pieces (rooks, queens, or bishops) at different starting positions, and you need to count how many different ways they can all move at once without crashing into each other.

The Rules:

  • Rooks move horizontally or vertically (4 directions) โ†‘โ†“โ†โ†’
  • Queens move in any direction (8 directions) โ†‘โ†“โ†โ†’โ†–โ†—โ†™โ†˜
  • Bishops move diagonally only (4 directions) โ†–โ†—โ†™โ†˜

Movement mechanics: All pieces start moving at time 0 and travel one square per second toward their chosen destinations. You can even choose a piece's current position as its destination (staying put).

Goal: Count all valid move combinations where no two pieces occupy the same square at any point in time during their journeys.

Input: Array of piece types and their 1-based starting positions
Output: Number of valid simultaneous move combinations

Input & Output

example_1.py โ€” Single Rook
$ Input: pieces = ["rook"], positions = [[1,1]]
โ€บ Output: 15
๐Ÿ’ก Note: A rook at (1,1) can stay put or move to any of the 14 squares in its row/column. Each choice is a valid move combination since there's only one piece, so total = 1 + 14 = 15 combinations.
example_2.py โ€” Two Rooks
$ Input: pieces = ["rook","rook"], positions = [[1,1],[8,8]]
โ€บ Output: 91
๐Ÿ’ก Note: Two rooks at opposite corners. They can move independently without collision in most cases. Each rook has 15 possible destinations, but we need to subtract combinations where they end up on the same square or collide during movement.
example_3.py โ€” Queen and Bishop
$ Input: pieces = ["queen","bishop"], positions = [[1,1],[2,2]]
โ€บ Output: 22
๐Ÿ’ก Note: Queen at (1,1) can move in 8 directions to 21 squares plus stay put = 22 destinations. Bishop at (2,2) can move diagonally. Need to count combinations where their paths don't intersect.

Constraints

Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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