answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<conio.h>

#include<math.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("\n\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(int n)

{

int k=1;

a[k]=0;

while(k!=0)

{

a[k]=a[k]+1;

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 i,n;

clrscr();

printf("Enter the number of Queens\n");

scanf("%d",&n);

queen(n);

printf("\nTotal solutions=%d",count);

getch();

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

The following program can be found by searching WikiBooks under Algorithm, Implementation, Miscellaneous, N-Queens.

#include <iostream>

using namespace std;

const int N = 5;

int position[N];

// Check if a position is safe

bool isSafe(int queen_number, int row_position)

{

// Check each queen before this one

for (int i = 0; i < queen_number; i++)

{

// Get another queen's row_position

int other_row_pos = position[i];

// Now check if they're in the same row or diagonals

if (other_row_pos N) // We placed N-1 queens (0 included), problem solved!

{

// Solution found!

cout << "Solution: ";

for (int i = 0; i < N; i++)

cout << position[i] << " ";

cout << endl;

}

else

{

for (int i = 0; i < N; i++) // Generate ALL combinations

{

// Before putting a queen (the k-th queen) into a row, test it for safeness

if (isSafe(k, i))

{

position[k] = i;

// Place another queen

solve(k + 1);

}

}

}

}

int main()

{

solve(0);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The following program can be found by searching WikiBooks under Algorithm, Implementation, Miscellaneous, N-Queens.

#include

using namespace std;

const int N = 5;

int position[N];

// Check if a position is safe

bool isSafe(int queen_number, int row_position)

{

// Check each queen before this one

for (int i = 0; i < queen_number; i++)

{

// Get another queen's row_position

int other_row_pos = position[i];

// Now check if they're in the same row or diagonals

if (other_row_pos N) // We placed N-1 queens (0 included), problem solved!

{

// Solution found!

cout << "Solution: ";

for (int i = 0; i < N; i++)

cout << position[i] << " ";

cout << endl;

}

else

{

for (int i = 0; i < N; i++) // Generate ALL combinations

{

// Before putting a queen (the k-th queen) into a row, test it for safeness

if (isSafe(k, i))

{

position[k] = i;

// Place another queen

solve(k + 1);

}

}

}

}

int main()

{

solve(0);

return 0;

}



This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include<stdio.h>

int main()

{

printf("WRONG PERSON,I DONT KNOW");

getch();

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program in c plus plus that implements the n-queens problem with backtracking?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the importance of stack algorithm in your program?

Stack implementations allow us to easily implement backtracking algorithms.


Who implements a unit's safety and health program?

Unit Commander


What DoD instruction implements the DoD CUI program?

The Department of Defense (DoD) instruction number 8500.01 implements the DoD Controlled Unclassified Information (CUI) Program. This instruction establishes the program and outlines the responsibilities of the Program Manager, the Designated Approving Authorities, and the CUI Program Office. The instruction also establishes the requirements for labeling, safeguarding, and disseminating CUI.


What is a method of developing software in which the programmer writes and implements a program in segments instead of waiting until the entire program is completed?

RAD (rapid application development)


What are the duties of a Program Integrator?

&bull; Acts as the "eyes and ears" of the PM &bull; Leads and directs the program support team (PST) &bull; Provides feedback and data to the PM &bull; Develops and implements program surveillance plans


Can you write a program that implements cramers rule for 2x2 and 3x3 matrix?

Yes I can. I did it in QBasic about 15 years ago.


What is the difference between agent function and agent program in AI?

From what I understand, agent function is a program that maps the percepts to action and update the internal state. an agent program actually implements the agent function


What is the relation between problem and programming?

Problem -&gt; Programming Programming can be a solution to a problem. If you have a problem and it can be solved by a computer program, so you can create such a program - so you can solve this problem by programming.


What is solution in c program?

The program itself is the solution. All programs are a solution to a given problem; that's the entire point of writing a program, to solve a problem. The program's algorithm specifies how the problem is solved and it's the programmer's job to convert that algorithm into working code.


What is the functinon of software patches?

A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.A patch is a fix for a problem in a computer program. When problems are found patches are applied and updates can later be prepared to resolve the problem completely.


What is structured overlay problem in program language?

what is over lay problem in programming design


What are the five stages in program development and its definition?

Analysis: Gather and analyze requirements for the program. Design: Plan the structure and components of the program. Implementation: Write the actual code for the program based on the design. Testing: Verify that the program functions as intended and fix any bugs. Maintenance: Update and maintain the program to ensure continued functionality and relevance.