Chessboard Region Attacker - Problem

Given an N×N chessboard and K bishops, place all K bishops on the board such that no two bishops attack each other diagonally.

Two bishops attack each other if they are on the same diagonal (either main diagonal or anti-diagonal). Your task is to count the total number of valid arrangements where all K bishops can be placed without attacking each other.

Note: Bishops move diagonally any number of squares. A bishop at position (i, j) attacks all positions (i+x, j+x), (i+x, j-x), (i-x, j+x), and (i-x, j-x) where the resulting position is within the board bounds.

Input & Output

Example 1 — Small Board
$ Input: n = 3, k = 2
Output: 26
💡 Note: On a 3×3 board, we can place 2 non-attacking bishops in 26 different ways. Bishops placed on different colored squares never attack each other.
Example 2 — Single Bishop
$ Input: n = 4, k = 1
Output: 16
💡 Note: With only 1 bishop on a 4×4 board, we can place it on any of the 16 squares, so there are 16 valid arrangements.
Example 3 — Impossible Case
$ Input: n = 2, k = 5
Output: 0
💡 Note: On a 2×2 board, we can place at most 2 bishops (one on each color). Placing 5 bishops is impossible.

Constraints

  • 1 ≤ n ≤ 10
  • 0 ≤ k ≤ n²

Visualization

Tap to expand
INPUTALGORITHMRESULT3×3 Chessboard, K=2Place 2 bishopsNo diagonal attacks1Separate by ColorSplit black/white squares2Try All Distributions0+2, 1+1, 2+0 bishops3Backtrack Each ColorCount valid arrangements4Combine ResultsMultiply and sum all options26Valid ArrangementsExamples:• Both on black squares: 10• One black, one white: 16• Both on white squares: 0(white squares form pairs)Key Insight:Bishops on different colored squares never attack each other, allowing us to split the problem into independent subproblems and dramatically reduce the search space.TutorialsPoint - Chessboard Region Attacker | Optimized Color Separation
Asked in
Google 15 Facebook 12 Microsoft 8
23.4K Views
Medium Frequency
~35 min Avg. Time
890 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