## N-Queens ProblemN - Queens problem is to place n - queens in such a manner on an n x n chessboard that no queens attack each other by being in the same row, column or diagonal. It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2 and n =3. So first we will consider the 4 queens problem and then generate it to n - queens problem. Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4. Since, we have to place 4 queens such as q Now, we place queen q The implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows: Fig shows the complete state space for 4 - queens problem. But we can use backtracking method to generate the necessary node and stop if the next node violates the rule, i.e., if two queens are attacking.
It can be seen that all the solutions to the 4 queens problem can be represented as 4 - tuples (x One possible solution for 8 queens problem is shown in fig: Place (k, i) returns a Boolean value that is true if the kth queen can be placed in column i. It tests both whether i is distinct from all previous costs x Using place, we give a precise solution to then n- queens problem. Place (k, i) return true if a queen can be placed in the kth row and ith column otherwise return is false. x [] is a global array whose final k - 1 values have been set. Abs (r) returns the absolute value of r. Next TopicMST Introduction |