Number of Valid Move Combinations On Chessboard Problem
Description
LeetCode Problem 2056.
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 i^th piece. In addition, you are given a 2D integer array positions also of length n, where positions[i] = [r_i, c_i] indicates that the i^th piece is currently at the 1-based coordinate (r_i, c_i) 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 0^th 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.
Notes:
- 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.
Example 1:

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

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

Input: pieces = ["bishop"], positions = [[4,3]]
Output: 12
Explanation: The image above shows the possible squares the piece can move to.
Constraints:
n == pieces.lengthn == positions.length1 <= n <= 4piecesonly contains the strings"rook","queen", and"bishop".- There will be at most one queen on the chessboard.
1 <= r_i, c_i <= 8- Each
positions[i]is distinct.
Solution
Approach: DFS
There are at most 4 pieces and each has at most 8 directions, so we can try every combination of moves with a DFS over the pieces.
For piece i we choose either to stay put or a direction and a distance. We record dist[i][x][y], the time at which piece i passes through square (x, y), and end[i], its final square and the time it gets there.
While extending piece i one step to (x, y) at time t we need two checks against the earlier pieces j < i:
checkPass: the piece may enter(x, y)at timetonly if no earlier piece is there at exactly that time and no earlier piece has already stopped there by timet.checkStop: the piece may stop at(x, y)at timetonly if every earlier piece passes through that square strictly beforet.
When a stop is valid, recurse to piece i + 1; when all pieces are placed, count one combination.
Complexity: time O((n * M)^n), space O(n * M), where n is the number of pieces and M the number of squares a piece can reach.
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]]:
if piece[0] == "r":
return rook_dirs
if piece[0] == "b":
return bishop_dirs
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