The problem Input integer n Place n queens on n x n board Q means queen at cell . Means empty space Queen can move in any direction Horizontally Up Down Diagonal neg + pos The Trick, let's say for 4x4 Understanding Notice that each and every queen has to be in a different row ! Notice that each and every queen has to be in a different column ! Notice that each and every queen has to be in a different positive diagonal ! Notice that each and every queen has to be in a different negative diagonal ! State - Set - is queen in column, posDiag (row-col), negDiag (row+col) Which rows have queen - do not have to store in state we just loop the rows Which columns have queen - store in state ! Which posDiag have queen - store in state! Which negDiag have queen - store in state! Trick posDiag -> row - column = constant Every time we increase row we increase column negDiag -> row + column = constant Every time we increase row we decrease column Loop 1st queen 1st row Can put first q
Software Engineering Best Practices, System Design, High Scale, Algorithms, Math, Programming Languages, Statistics, Machine Learning, Databases, Front Ends, Frameworks, Low Level Machine Structure, Papers and Computing, Computer Science Book Reviews - Everything!