Rahul-Vijay / C_plus_plus_Algos

Collection of C++ Algorithms for a quick reference. Fork it and start hacking :)
0 stars 32 forks source link

N-queens #29

Closed adityaneel94 closed 5 years ago

adityaneel94 commented 5 years ago

prints the number of possible solutions of n-queens puzzle for placing n queens in an nxn chessboard.

Rahul-Vijay commented 5 years ago

@adityaN33L can you provide some description ?

adityaneel94 commented 5 years ago

Sure. The program takes n (number of queens to be placed or the size of the chessboard as input). The board is represented as a vector of strings with '.'(dot) at unoccupied positions and 'Q' where the queen can be placed. 'nqueens( )' works recursively by placing a queen in (i)th row of the 'col' column and then finds recursively in which row the next queen can be placed for the (col+1)th column. For every position the queen is placed, 'isSafe( )' function is called to check the validity of that placement because none of the queens should be aligned in any way possible. If that placement is not safe you backtrack and find another row in that column to place the queen. 'cnt' variable keeps the count of how many such successful orientations of n-queens are possible.

adityaneel94 commented 5 years ago

Let me add this description to the code as well. Regards.

Rahul-Vijay commented 5 years ago

Thanks :)