I think this problem is a huge can of worms
I found this site
http://home.halden.net/vkp/vkp/layouts.htmlIt seems you can either go the random route(often unwinnable)or set up many predefined patterns with no algorithm(always winnable).
This though may help
http://www.formauri.es/personal/pgimeno/mj/mjsol.htmlHope this clears up the nature of your problem.