8 queens problem

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
User avatar
narke
Member
Member
Posts: 119
Joined: Wed Dec 26, 2007 3:37 am
Location: France

8 queens problem

Post by narke »

One thing interests me,
I tried to solve the 8 queens problem all by myself, without using a known solution.
My algo only achieved to place 7 queens.
Do you consider that this problem is too easy or quite hard?

Solutions in various languages are here: http://rosettacode.org/wiki/N-queens_problem
OS for PowerPC Macs: https://github.com/narke/Einherjar
Operating system: colorForth computing environment for x86.: https://github.com/narke/Roentgenium
User avatar
iansjack
Member
Member
Posts: 4683
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: 8 queens problem

Post by iansjack »

Pick the right tool for the problem and it should be trivial. I would pick Prolog.
User avatar
narke
Member
Member
Posts: 119
Joined: Wed Dec 26, 2007 3:37 am
Location: France

Re: 8 queens problem

Post by narke »

I used Python but I'm interested to know how OSdevers rate the complexity of this problem.
I know that one can tell me that one must use recursion and it becomes trivial but when you must figure it out by yourself it becomes a bit trickier than watching an existing solution and saying "I understood".
OS for PowerPC Macs: https://github.com/narke/Einherjar
Operating system: colorForth computing environment for x86.: https://github.com/narke/Roentgenium
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: 8 queens problem

Post by Combuster »

It's a fairly typical constraint satisfaction problem you can solve with textbook algorithms like DFS.

It's also a problem you can optimise by looking at the problem in particular: you don't need to try all 64 places for each queen, (or the subset that remains available at that point), but only a subset of eight for any particular queen as there is exactly one on each row.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: 8 queens problem

Post by bwat »

narke wrote:I used Python but I'm interested to know how OSdevers rate the complexity of this problem.
I know that one can tell me that one must use recursion and it becomes trivial
It is trivial, hence the simplicity of the Prolog "generate and test" type of solution, i.e., find a permutation of N queens (generalised from 8 to N queens) on a chess board [generate], now test to see if none attack each other, upon failure backtrack to find a new permutation. Eventually all permutations will have been attempted. Imperative/functional programmers will use loops/recursion for a simple solution instead of backtracking.

Obviously this takes time and after a while the programmer starts to think about heuristics to reduce the search space [see Combuster's answer].
narke wrote: but when you must figure it out by yourself it becomes a bit trickier than watching an existing solution and saying "I understood".
Problems like this are the bread and butter of the Prolog programmer and are used as homework exercises at universities around the world. It only seems tricky when you haven't done a few of them. Give it a proper try and don't feel ashamed if it takes a loooong time to solve. The next problem of this type you attempt will see the answer come more quickly. See http://dtai.cs.kuleuven.be/ppcbook/ for a free book chock full of these types of problems [you don't need to solve them with Prolog].
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
hometue
Member
Member
Posts: 100
Joined: Thu Dec 19, 2013 1:40 am
Location: Asia, Singapore

Re: 8 queens problem

Post by hometue »

If I remember this well it was covered in competitive programming. (the book title is competitive programming). If I am not wrong we use...uh...brute force (aka backtracking), but it can be optimized to prevent using every possibilities.

@combustor: how do you use DFS? I don't see any way...
CookieOS. Want a cookie? Its only black and white for now though, probably as bad as my baking skills.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: 8 queens problem

Post by Combuster »

You need to place 8 queens. You can view that as a tree where the root is an empty board and every step away from the root adds a single queen (in an available position). The application of DFS easily follows from there.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
hometue
Member
Member
Posts: 100
Joined: Thu Dec 19, 2013 1:40 am
Location: Asia, Singapore

Re: 8 queens problem

Post by hometue »

Hm...it seems fine but as a competitive programmer, I wonder performance wise which would be better (I know usually DFS would be better but you will never know). Also looking at it I can see a slight possible problem where the programmer totally forgets about the root. Btw, I am wondering based on that logic would it be BFS or DFS, since we are going a step from the root at one time BFS sounds like a good solution too.
CookieOS. Want a cookie? Its only black and white for now though, probably as bad as my baking skills.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: 8 queens problem

Post by Combuster »

Breadth-first only works if the solution is close to the starting position. In the typical case and this case in particular it's a bad idea because you need to find (and keep in memory) all of the 7-queen solutions before you are even allowed by the algorithm to find the 8-queen solutions.

If you need all solutions, DFS finds everything in the same time as BFS, with less memory use. If you need one solution, BFS is a gross horror.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AndrewAPrice
Member
Member
Posts: 2298
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Re: 8 queens problem

Post by AndrewAPrice »

I would probably do a brute force algorithm, but rather than working out all ~4 billion permutations, you could do an early termination (mark all cells queen 1 can see as invalid, only iterate queen 2 over valid cells, mark all queen 1 & 2 see as invalid), also you can cut the permutations down significantly by only placing higher queens in front of lower queens (to avoid repeating old permutations).
My OS is Perception.
Post Reply