I think these is usefull: link1 link2
For Best result you should design your algorithm by dynomic-programing and can find it in: google Stackoverflow
you set an array a[n][count] and save state i in a[..][i]. this is sample for item first by n=8 :
a = {5,1,8,4,2,7,3,6}{1} , .....
Note: each solution can changed to 8 state by symmetry and rotation.So for each result you symmetry and rotation their solution to check is that save in your array or not?