Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hello all,

I'm looking for a C/C++/C#/Perl implementation of the solution to the "8 queens" problem via a "Hill Climbing" algorithm.

I found tons of theoretical explanations on that specific issue on the web but not a single code example.

A moment before I sit down to code this piece of AI myself, I wanted to ask if any of you good people can point me to what I'm looking for.

Thanks in advance,
Matthew.S.
Posted

1 solution

Hi Matthew,

this kind of smells like homework, but I'll give you a couple of hints anyhow.
I hope you understand the 8 queens problem so I wont go into that.

"Hill Climbing" algorithms start at a randomly selected start point, and
try to do small gradual optimizations trying to obtain a solution (which
may not even exist).

So I'd start this by placing the first queen on a randomly selected position
on the board.

Place the next queen on the board (randomly of course). Two things can happen now:
1. No collision of queens -> proceed with next queen
2. Queens collide -> move queen to next available position and re-check until either there are no more available positions or the collision is resolved.
(Remember to wrap around to top left corner when leaving bottom right corner of board until starting position of queen is reached)

If a queen was moved all over the board without finding a collision free position. The previously placed queen needs to be repositioned (randomly of course).

This should give you a pretty good idea to start implementing a straight forward algorithm.

Best Regards,

Manfred
 
Share this answer
 
v3
Comments
sigurko 22-Nov-10 14:54pm    
Thanks Manfred!!

You are right, this is a homework. Your simple explanation cleared the picture.

-Matthew

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900