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

  • Home
  • SEARCH
  • 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 7489497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:15:25+00:00 2026-05-29T15:15:25+00:00

int placement[] = new int[8]; int placeQueen(int row) { if(row == 8) { printBoard();

  • 0
int placement[] = new int[8];
int placeQueen(int row)
{
   if(row == 8)
   {
      printBoard();
      return;
   }

   for(int i = 0; i<8; i++)
   {
      placement[row] = i;
      if(check(row))
      {
         placeQueen(row + 1);  \\This step
      }
   }
}

boolean check(int row)
{
   for(int i = 0; i<row; i++)
   {
      int diff = Math.abs(placement[row] - placement[i]);
      if(diff == 0 || (diff == row - i))
      {
         return false;
      }
   }
return true;
}

What I exactly don’t get is how the recursion works inside the for loop.

  • 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-05-29T15:15:27+00:00Added an answer on May 29, 2026 at 3:15 pm

    This algorithm recurses over the rows. It tries adding one queen on row r at each of eight positions it could be at in the row. Trying this is sufficient because if two queens were placed on the same row you’d violate the condition that no two queens could attack each other.

    Since one queen is placed at a time, it is sufficient for the check function to only check that the newly placed queen is not able to attack any of the previously placed queens. diff == 0 makes sure the new queen isn’t in the same position as any of the previous queens. diff == row - i ensures the new queen can’t attack another along either diagonal movements. (This catches both diagonal movements because of the Math.abs() in the preceding line.)

    So, again:
    Try placing one queen at a time, see if it can’t attack any of the others. If it can, return immediately — otherwise try adding a queen in the next row in all ways possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

int x = n / 3; // <-- make this faster // for instance
int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
According to this reference for operator new : Global dynamic storage operator functions are
I was just wondering how placement new would behave when it is passed a
I've got this trivial class hierarchy: class Base { public: virtual int x( )
Is it possible to actually make use of placement new in portable code when
I'm new, so sorry if this turns out so simple that I should have
Can I call the C++ placement new on constructors with parameters? I am implementing
I have been trying to understand the Placement new concept. I searched on the
int x; printf(hello %n World\n, &x); printf(%d\n, x);

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.