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 8710129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:32:41+00:00 2026-06-13T04:32:41+00:00

This program for n-queens uses a weird logic for backtracking. I tried to track

  • 0

This program for n-queens uses a weird logic for backtracking. I tried to track the code many times but i always get confused with it. I am really confused with the Place(int pos) function.

#include<stdio.h>
#include<conio.h>
int a[30],count=0;
int place(int pos)
{
    int i;
    for(i=1;i<pos;i++)
    {
        if( (a[i]==a[pos]) || ((abs(a[i]-a[pos])==abs(i-pos))) )
        {
            return 0;
        }
    }
    return 1;
}

void print_sol(int n)
{
    int i,j;
    count++;
    printf("\nSOLUTION #%d\n",count);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(a[i]==j)
                printf("Q\t");
            else
                printf("*\t");
        }
        printf("\n");
    }
}



void queen(n)
{
    int k=1;
    a[k]=0;
    while(k!=0)
    {
        a[k]++;
        while(a[k]<=n && !place(k))
            a[k]++;
        if(a[k]<=n)
        {
            if(k==n)
                print_sol(n);
            else
            {
                k++;
                a[k]=0;
            }
        }
        else
            k--;
    }
}

void main()
{
    int n;
    clrscr();
    printf("\nEnter the number of queens:");
    scanf("%d",&n);
    queen(n);
    getch();
}

I just want to know how it backtracks automatically?

  • 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-13T04:32:43+00:00Added an answer on June 13, 2026 at 4:32 am

    place merely checks if the queen in row pos can be placed in column a[pos].

    The backtracking is in the queens function, when trying to place the queen in row k, initially, a[k] is 0, which is not a valid position for the queen

    a[k]++;     // here, a[k] was not a valid position and no smaller was valid, so increment
    while(a[k]<=n && !place(k))  // while the position is invalid, check next
        a[k]++;
    if(a[k]<=n)  // if a valid column was found
    {
        if(k==n) // that was the last row, done
            print_sol(n);
        else     // next row
        {
            k++;
            a[k]=0;
        }
    }
    else  // no possible column found (a[k] > n), so backtrack
    k--;  // check for next possible column in previous row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program runs just fine, but for some reason when I ask for an
This program stores pairs in a map, counting the number of times a word
this program seems be fine but I still getting an erro, some suggestion? Program:
This program builds with no problem, and the executable starts up, but no triangle
This program compiles fine but seg faults when running the value->getSmall in Period::display() .
this program crashes if i uncomment the release statements. i get that i am
This program is to use the keyboard keys to play notes. I get a
This program is running but how to send multiple CC and BCC. for($i =
This program gives 6 as output, but when I uncomment the line 9, output
This program builds a dictionary out of a list based on what two index

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.