Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:29:21+00:00 2026-06-02T20:29:21+00:00

Given k rooks and a n by n chess board, the rooks can safely

  • 0

Given k rooks and a n by n chess board, the rooks can safely be placed on the board W different ways, where

W = k!(n C k)^2
written differently W = n!n!/(k!(n-k)!(n-k)!)

PROBLEM STATEMENT:

Write a program that will run over a n by n chessboard and count all the ways that k rooks can safely be placed on the board.

MY RESEARCH:

After searching the internet I finally find a nQueensSolution code on Geekviewpoint and I modify it as below. However my code only works when k = n. Does anyone have an idea how to solve this for k<n?

Here is my code:

static int kRooksPermutations(int[] Q, int col, int k, int kLimit) {
    int count = 0;
    for (int x = 0; x < Q.length && col < Q.length; x++)
        if (safeToAdd(Q, x, col)) {
            if (k == kLimit - 1) {
                count++;
                Q[col] = -1;
            } else {
                Q[col] = x;
                count += kRooksPermutations(Q, col + 1, k + 1, kLimit);
            }
        }
    return count;
}//

static boolean safeToAdd(int[] Q, int r, int c) {
    for (int y = 0; y < c; y++)
        if (Q[y] == r)
            return false;
    return true;
}//

Here is a test code

public static void main(String... strings) {
    kRooksPermutations(8,5);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:29:22+00:00Added an answer on June 2, 2026 at 8:29 pm

    Got it!

      // Empty
      static final int MT = -1;
    
      static int kRooksPermutations(int[] Q, int col, int rooksInHand) {
        // Are we at the last col?
        if (col >= Q.length) {
          // If we've placed K rooks then its a good'n.
          return rooksInHand == 0 ? 1 : 0;
        }
    
        // Count at this level starts at 0
        int count = 0;
        // Have we run out of rooks?
        if (rooksInHand > 0) {
          // No! Try putting one in each row in this column.
          for (int row = 0; row < Q.length; row++) {
            // Can a rook be placed here?
            if (safeToAdd(Q, row, col)) {
              // Mark this spot occupied.
              Q[col] = row;
              // Recurse to the next column with one less rook.
              count += kRooksPermutations(Q, col + 1, rooksInHand - 1);
              // No longer occupied.
              Q[col] = MT;
            }
          }
        }
        // Also try NOT putting a rook in this column.
        count += kRooksPermutations(Q, col + 1, rooksInHand);
    
        return count;
      }
    
      static boolean safeToAdd(int[] Q, int row, int col) {
        // Unoccupied!
        if (Q[col] != MT) {
          return false;
        }
        // Do any columns have a rook in this row?
        // Could probably stop at col here rather than Q.length
        for (int c = 0; c < Q.length; c++) {
          if (Q[c] == row) {
            // Yes!
            return false;
          }
        }
        // All clear.
        return true;
      }
    
      // Main entry - Build the array and start it all going.
      private static void kRooksPermutations(int N, int K) {
        // One for each column of the board.
        // Contains the row number in which a rook is placed or -1 (MT) if the column is empty.
        final int[] Q = new int[N];
        // Start all empty.
        Arrays.fill(Q, MT);
        // Start at column 0 with no rooks placed.
        int perms = kRooksPermutations(Q, 0, K);
        // Print it.
        System.out.println("Perms for N = " + N + " K = " + K + " = " + perms);
      }
    
      public static void main(String[] args) {
        kRooksPermutations(8, 1);
        kRooksPermutations(8, 2);
        kRooksPermutations(8, 3);
        kRooksPermutations(8, 4);
        kRooksPermutations(8, 5);
        kRooksPermutations(8, 6);
        kRooksPermutations(8, 7);
        kRooksPermutations(8, 8);
      }
    

    Prints:

    Perms for N = 8 K = 1 = 64
    Perms for N = 8 K = 2 = 1568
    Perms for N = 8 K = 3 = 18816
    Perms for N = 8 K = 4 = 117600
    Perms for N = 8 K = 5 = 376320
    Perms for N = 8 K = 6 = 564480
    Perms for N = 8 K = 7 = 322560
    Perms for N = 8 K = 8 = 40320
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a package, how can I automatically find all its sub-packages?
Given Java's write once, run anywhere paradigm and the fact that the Java tutorials
Given an NSApp object (aka [NSApplication sharedApplication]), how can I get the currently active
I've been trying to work out this particular problem and though I can easily
implementing code for customized mini(less squares, less pieces ) chess board in java or
I'm implementing a chess game in c++ and some of the classes are Board
Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given a date range how to calculate the number of weekends partially or wholly
Given an interface or interfaces, what is the best way to generate an class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.