Writing a Recursive Function for Placing n Queens on an n x n Chessboard

In summary, the conversation discusses an assignment to write a recursive function for placing n Queens on an n x n chessboard, which was not difficult to figure out. The person is also looking for help with writing another function to find all possible solutions for safely placing the Queens. They clarify that by solutions they mean all possible ways to place the Queens without them attacking each other. A google search is suggested as a resource for this task.
  • #1
discoverer02
138
1
I have an assignment to write a recursive function that will safely place n Queens on an n x n chessboard. This wasn't all that difficult to figure out.

For extra credit I'm supposed to write another function(s) (recursive?) that figures out all the possible solutions. This is, so far, giving me fits.

Any nudge in the right direction would be greatly appreciated.
 
Last edited:
Computer science news on Phys.org
  • #2
Can you be a little more specific in what you mean by figure out all the possible solutions? Do you mean once you place all the queens on the board, find all the possible moves which lead to a checkmate? That might take a while, especially if you have a bunch of queens. It is on the exponential scale.
 
  • #3
Sorry for not being clear. By solutions, I mean all the possible ways I can place the Queens safely. ie. No Queen can attack another Queen on the board.
 
  • #5
Thanks dduardo.
 

1. How does a recursive function work?

A recursive function is a function that calls itself until a certain condition is met. In the case of placing n queens on an n x n chessboard, the function would call itself to place each queen on the board, until all n queens are placed without attacking each other.

2. What is the base case in a recursive function for placing n queens on an n x n chessboard?

The base case in this recursive function would be when all n queens have been successfully placed on the chessboard without attacking each other. This means that the function would stop calling itself and return the solution.

3. How does the recursive function handle backtracking?

Backtracking is a technique used in recursive functions to undo previous steps and try a different approach. In the case of placing n queens on an n x n chessboard, if the function reaches a point where a queen cannot be placed without attacking another queen, it will backtrack and try a different position for the previous queen.

4. Can a recursive function for placing n queens on an n x n chessboard be optimized?

Yes, a recursive function for this problem can be optimized by using pruning techniques. This means that the function would not continue to explore branches that are not feasible, reducing the number of recursive calls and improving efficiency.

5. Is there a limit to the size of n for which the recursive function can be used?

Theoretically, there is no limit to the size of n for which the recursive function can be used. However, as n increases, the time and memory complexity of the function also increase significantly. This means that for very large values of n, the function may not be practical to use and a different approach may be needed.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
11
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Replies
4
Views
386
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Calculus and Beyond Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
28
Views
5K
Replies
7
Views
1K
  • General Math
Replies
16
Views
2K
Back
Top