Sun 27 Aug 2006
A common riddle in the homework of A.I courses homework is this:
Given an NxN board, you need too arrange N queens such that no queen threatens any other queen.
Of course I write about it because I got this question in the homework of my Intro to A.I course.
The algorithm that solves this riddle efficently is a random and greedy:
Since there are N rows and N queens, there must be a queen in each column (why?),
so we put a queen in each row, but we also need to pick a row for each queen.
the idea is to randomize the rows at the begining, and then, in each iteration move one queen that reduces the number of threatened queens by a maximum.
We do it in each iteration, until the number if threatened queens reach 0, or we get to a situation where we can’t improve any more, because we had a bad start - so we start over, randomizing locations again, and doing the whole thing again, until the queens a safe spot.
The question was not algorithmic but implementational, the algorithm was desribed in the question very clearly, I only need to pick a language and implement it.
unlike my previous assignment, where I choose Prolog to implement Sudoku, which took a long time and brought questionable results, this time I decided to go for the home language - Java.
in about an hour it was written, except it didn’t work - sometimes the algorithm stuck in an infinite loop.
initially I printed the board to the console, but I quickly realized I wont get far this way:
its very hard to detect repeating patterns when they are printed as text, and its also hard to see if the queens threatens diagonaly.
I decided to do wheat I do well, and wrote a User interface that show a simulation of the algorithm, and with it I found and fixed the bugs.
I thought to myself, If I have got this far, I might as well invest some more and make it more useful:
I turned it into an Applet, to allow running it also in the browser, and added a few buttons to control the simultion.
this is the result:
Try to play with it, every time it gets stuck the background turns red and it randomize positions again.
Note that you can solve very big boards with it.
more things to notice are that there is a problem with small board (how small?) and that every time it gives a different solution because the start is random.
the applet is here, the code is included.
April 1st, 2007 at 10:36 pm
excellent explanation
August 24th, 2007 at 12:38 pm
Good Implementation; Try to explain the algorithm also
October 11th, 2007 at 3:06 am
Sudoku Games Strategy…
Sorry, it just sounds like a crazy idea for me :)…
November 28th, 2007 at 8:38 am
Website Templates and Web Design, Graphic Layouts…
Sorry, it just sounds like a crazy idea for me :)…
December 5th, 2007 at 8:30 pm
Computer Game News and Game Reviews…
Sorry, it just sounds like a crazy idea for me :)…
January 14th, 2008 at 12:23 am
Have try 50!
January 31st, 2008 at 9:38 pm
hi, can you please tell me how I can have a look to the source code?
February 7th, 2008 at 12:35 pm
Good Implementation
February 25th, 2008 at 6:01 am
hi, can you please tell me how I can have a look to the source code?
March 2nd, 2008 at 3:31 am
Hi,
where can i get the source code from?
can i have a look at it