Minimum Moves to Get a Peaceful Board - Problem
Minimum Moves to Get a Peaceful Board
You are given an
Your goal is to rearrange the rooks so that the board becomes peaceful. A board is peaceful when there is exactly one rook in each row and exactly one rook in each column.
You can move rooks one cell at a time, either vertically or horizontally to adjacent cells. However, no two rooks can occupy the same cell at any point during the moves.
Return the minimum number of moves required to make the board peaceful.
Example: If you have 3 rooks on a 3ร3 board at positions [[0,0], [1,0], [2,2]], you need to move them so each row and column has exactly one rook, like [[0,0], [1,1], [2,2]].
You are given an
n ร n chessboard with n rooks placed on it. The positions of the rooks are given as a 2D array rooks where rooks[i] = [xi, yi] represents the position of the i-th rook.Your goal is to rearrange the rooks so that the board becomes peaceful. A board is peaceful when there is exactly one rook in each row and exactly one rook in each column.
You can move rooks one cell at a time, either vertically or horizontally to adjacent cells. However, no two rooks can occupy the same cell at any point during the moves.
Return the minimum number of moves required to make the board peaceful.
Example: If you have 3 rooks on a 3ร3 board at positions [[0,0], [1,0], [2,2]], you need to move them so each row and column has exactly one rook, like [[0,0], [1,1], [2,2]].
Input & Output
example_1.py โ Simple Case
$
Input:
rooks = [[0,0],[1,0],[2,2]]
โบ
Output:
1
๐ก Note:
Move the rook from (1,0) to (1,1). Now we have rooks at (0,0), (1,1), (2,2) - each row and column has exactly one rook.
example_2.py โ All Same Row
$
Input:
rooks = [[0,0],[0,1],[0,2]]
โบ
Output:
3
๐ก Note:
All rooks are in row 0. We need to move them to (0,0), (1,1), (2,2). Total moves: |0-1| + |1-1| + |2-2| + |0-0| + |1-1| + |2-2| = 1 + 0 + 0 + 0 + 0 + 0 = 3 moves (actually 0 + 3 = 3, where 3 comes from row moves)
example_3.py โ Already Peaceful
$
Input:
rooks = [[0,0],[1,1],[2,2]]
โบ
Output:
0
๐ก Note:
The board is already peaceful - each row and column has exactly one rook, so no moves are needed.
Constraints
- n == rooks.length
- 1 โค n โค 100
- rooks[i].length == 2
- 0 โค xi, yi โค n - 1
- All the values of rooks are unique
- The input is guaranteed to have exactly n rooks on an n ร n board
Visualization
Tap to expand
Understanding the Visualization
1
Identify Current State
List all current rook positions and see which rows/columns have conflicts
2
Define Target
The peaceful arrangement has exactly one rook per row and per column
3
Apply Greedy Matching
Sort current positions and target positions, then match them optimally
Key Takeaway
๐ฏ Key Insight: Row and column assignments can be optimized independently using greedy matching of sorted positions, leading to minimum total Manhattan distance.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code