Nqueens problem, n will be input, output count of all possibles

In summary, the programmer is trying to find a position for a queen on a board, but is stuck due to the complexity of the recursive function.
  • #1
otnaug
2
0
Hi,

I'm writing a program for nqueens where inputs will be n and possibly a few existing queens already placed on the board. So far I've come up with one method, but got stuck on the second one.
(hoping second method is more efficient)

1st method:
- tabulate possible outcome of (n*n choose n)
- loop through all the outcomes and decide their legality.
- (if there are inputs of existing queens then that's great, i just narrow down from the possible outcome table)

2nd method (stuck):
- i want to do the brute force method on wikipedia -- going horizontal line by line on the board
- for nqueens i need to write n loops.
- so my program will have 20 different sections, 1 for each n up to 20 (with the 20 being a 20-level nested loop), once user input n i'll go to that loop.
- seems like a really bad idea.



any hint is welcome!
the assignment is to further parallelize it, i don't know how to parallelize recursive codes (not to mention i don't know how to write a recursive version of nqueens).



thanks!
 
Physics news on Phys.org
  • #2
- so my program will have 20 different sections, 1 for each n up to 20 (with the 20 being a 20-level nested loop), once user input n i'll go to that loop.
Looks like an application for a recursive function. Each function has a loop which can run in parallel.
 
  • #3
mfb said:
Looks like an application for a recursive function. Each function has a loop which can run in parallel.

can you provide some outlines for the recursive function? thanks!
 
  • #4
FindPosition(LineToFill, board) {
..//look for possible position for queen
..//if position possible, call FindPosition(LineToFill+1,modifiedboard)
}

Like that?
 
  • #5
Which language are you using?
 
  • #6
Moved to the homework forum. Be mindful of the guidelines for helping with assignments: no complete solutions, etc.
 

1. What is the Nqueens problem?

The Nqueens problem is a classic computer science problem that involves placing N chess queens on an N x N chessboard in such a way that no two queens can attack each other. This means that no two queens can be in the same row, column, or diagonal.

2. How do you solve the Nqueens problem?

The Nqueens problem can be solved using various algorithms such as backtracking, recursion, and genetic algorithms. The most commonly used approach is backtracking, which involves placing queens on the board one by one and checking if they are in a safe position. If not, the queen is removed and placed in a different position until a solution is found.

3. What is the input for the Nqueens problem?

The input for the Nqueens problem is the value of N, which represents the size of the chessboard and the number of queens to be placed.

4. What is the output of the Nqueens problem?

The output of the Nqueens problem is the total number of possible solutions to placing N queens on an N x N chessboard in a non-attacking position.

5. What is the time complexity of solving the Nqueens problem?

The time complexity of solving the Nqueens problem varies depending on the algorithm used. The backtracking algorithm has a worst-case time complexity of O(N!), while the genetic algorithm has a time complexity of O(N^2).

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top