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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:17:08+00:00 2026-06-07T14:17:08+00:00

I was solving the N Queen problem where we need to place N queens

  • 0

I was solving the N Queen problem where we need to place N queens on a N X N chess board such that no two queens can attack each other.

#include <stdio.h>  
#include <stdlib.h>
#include<math.h>

int size=8;
char arr[8][8];
int i,j;

void initializeBoard()
{
  for(i=0;i<size;i++)
  {
    for(j=0;j<size;j++)
    {
        arr[i][j]='.';
    }
  }
}

void printArray()
{

  for(i=0;i<size;i++)
  {

    for(j=0;j<size;j++)
    {
        printf("%c\t",arr[i][j]);
    }

    printf("\n");
  }
  printf("\n\n");
}

void placeQueen(int i,int j)
{
  arr[i][j]='Q';
}

int isAvailable(int i,int j)
{
   int m,n,flag;

   for(m=0;m<i;m++)
   {
      for(n=0;n<size;n++)
      {
        int k=abs(i-m);
        int l=abs(j-n);

        if(arr[m][j]!='Q' && arr[k][l]!='Q')
        {
            flag=1;
        }

        else
        {
            flag=0;
            break;
        }
    }
}
return flag;

}


int main(void)
{
    initializeBoard();

    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            if(isAvailable(i,j)==1)
            {
                // means that particular position is available
                // and so we place the queen there

                placeQueen(i,j);
                break;
            }
        }
    }

    printArray();
    return 0;
}

I think the problem is with the isAvailable() method. However, I am not able to find the bug. Please help me identify it.

Is the approach that i am taking involves backtracking ? If not, please provide the same with some explanations

  • 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-07T14:17:11+00:00Added an answer on June 7, 2026 at 2:17 pm

    Your approach does not backtrack. It iterates over some possibilities, not all. This problems is best solved recursively, so I wouldn’t do it as you are doing. You have to define the rules for a Queen being attacked by other. You do it using ifs, and recursion to apply the rule again and to iterate. Most of the backtracking algorithms are written recursively.
    I will give you an answer, so you can base yours on mine.

    #include <stdio.h>
    #include <stdlib.h>
    
    int count = 0;
    void solve(int n, int col, int *hist)
    {
        int i, j;
        if (col == n) {
            printf("\nNo. %d\n-----\n", ++count);
            for (i = 0; i < n; i++, putchar('\n'))
                for (j = 0; j < n; j++)
                    putchar(j == hist[i] ? 'Q' : ((i + j) & 1) ? ' ' : '.');
    
            return;
        }
    
    #   define attack(i, j) (hist[j] == i || abs(hist[j] - i) == col - j)
        for (int i = 0, j = 0; i < n; i++) {
            for (j = 0; j < col && !attack(i, j); j++);
            if (j < col) continue;
    
            hist[col] = i;
            solve(n, col + 1, hist);
        }
    }
    
    int main(int n, char **argv)
    {
        if (n <= 1 || (n = atoi(argv[1])) <= 0) n = 8;
        int hist[n];
        solve(n, 0, hist);
    }
    

    The way backtracking works in the following:

    1. create a constraint (a rule) to check if the conditions are meet.
    2. Consider the problem as a search tree. The time spent to search this tree is based on n, the size of the board. The best way to search is recursively, so have in mind, the smart way to solve is using recursion.
    3. In that code, the first set of for loops just prints the board out, and checks if Q if found.
    4. # define attack(i, j) (hist[j] == i || abs(hist[j] - i) == col - j) is my rule, which asserts 2 queens are not attacking each other.
    5. The second set of for loops finds a condition which another queen can be inserted, within the constraint rules.
    6. Then I call find function again. That’s how the backtracking is done.
    7. My base case is that 2 queens can be on the board, then I’m going recursively check if another queen can be added until 8. Thus, 2 + 1 = (1+1) + 1 = 1 (1 + 1). Applying the rule again, we have 3 + 1 = (2) + 1 + 1 = (2) + (1 + 1), and again 4 = (3) + 1 + 1 = (3) + (1+1).
    8. Recursion does that for you. Let out apply the rule over and over. So f(n+1) = f(n) + 1 for that case and f(2) = 2 is my base case.
    9. The base of backtracking is if one of those branches don’t work out, you can go one level up and search another branch, and so on, until the tree is all searched out. Again, recursion is the way to go.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was solving the N Queen problem where we need to place 4 queens
I am solving a Queen collision problem that can be located here I just
While solving a DP related problem, I observed that first works but the second
I was solving a Project Euler problem that goes as follows: By considering the
I'm solving a problem that consist in generate the first n<1000000 lucky numbers, but
I need help solving the problem below. I am creating a Windows Forms Application
Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
After solving the other problem with routes , now I have another one. I
I am solving the n Queens problem with open mp, (The original eight queens
I'm solving some problem that involves Rabin–Karp string search algorithm. This algorithm requires rolling

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.