29

Kata Stats: How many queens? | Codewars

 4 years ago
source link: https://www.codewars.com/kata/how-many-queens/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rethinking the n queens problem for SQL

This is a version of the classic n queens problem (https://en.wikipedia.org/wiki/Eight_queens_puzzle) but reimagined for SQL!

Imagine an NxN chessboard. You also have N queens. How many possible ways can you place all of the queens on the chessboard such that none of them are threatening eachother?

For example, with a 4x4 board and 4 queens, we can do the following placements:

- q - -
- - - q
q - - -
- - q -
- - q -
q - - -
- - - q
- q - -

Any other arrangement will lead to us not being able to place all 4 queens without at least one queen being threatened. Therefore, there are 2 solutions.

You should create a function nqueens(n int) which, given an integer n, returns the number of possible solutions on an n by n board with n queens. For example,

nqueens(4)

should return 2.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK