Skip to main content

Posts

Showing posts from September, 2014

N Queen problem

My favorite algorithm which includes in Recursion and backtracking method is N Queen problem.I love this problem because it helped me to solve various other problem including "Sudoku". The solution for this problem typically easy one,There are three types of attacks for the Queen to each other

This can be achieved by,if i,j are the respective coordinates which represents x = i and y = j

There are three types of attack

1. a[i] == a[j] same column or row

2. (a[i] - a[j]) - (i-j) same major diagonal

3. (a[j]-a[i]) - (j-i) same minor diagonal

The code for this problem.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57publicclassQueens{/*********************************************************************** * Return true if queen placement q[n] does not conflict with * other queens q[0] through q[n-1] ***********************************************…