algorithm - N-Queens puzzle solution - cannot understand -
i reading description solution n-queens puzzle on sicp , cannot understand of them. here solution:
one way solve puzzle work across board, placing queen in each column. once have placed k - 1 queens, must place kth queen in position not check of queens on board. can formulate approach recursively: assume have generated sequence of possible ways place k - 1 queens in first k - 1 columns of board. each of these ways, generate extended set of positions placing queen in each row of kth column. filter these, keeping positions queen in kth column safe respect other queens. produces sequence of ways place k queens in first k columns. continuing process, produce not 1 solution, solutions puzzle.
suppose 8 8 chessboard looks this: eyes destroyed cannot use pictures. 0 means no queen, 1 means queen.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
work across board, placing queen in each column.
my understanding columns read vertically , rows read horizontally. text mean this?
1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
i have placed queen in each column no rows specified, since done recursively assume generated ways of positions 2 queens not in check each other.
assume have generated sequence of possible ways place k - 1 queens in first k - 1 columns of board.
say k = 1. 1-1 = column 0 have 1 way of generating positions because it's empty board.
for each of these ways, generate extended set of positions placing queen in each row of kth column.
my solution column 0 1 way, absolutely have no idea following means.
generate extended set of positions placing queen in each row of kth column.
what "generate extended set of positions" , placing queen in each row of column mean? if k = 1?
1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
but of queens aren't safe because in same columns right?
i totally lost on how proceed. can explain me?
note: if give visual explanation, please provide textual explanation because can't see images , pictures. thanks
first of all, doesn't matter whether use columns or rows - output same, because problem symmetric. symmetry create confusion; prepared this.
without regard specific questions, idea here recursion. problem talks arrangement of 8 queens. if have placed k-1
queens, got "position". each position, can several "extended" positions, in have placed 1 more queen (so there k
queens). each set of positions k-1
queens, there set of positions k
queens.
this set should "filtered" - remove invalid positions it. in cases, empty (not possible place queen); in no way special situation - happen lot. in other cases (actually, majority of cases), big. example, "empty" position - no queens - there several (actually, 8 - see below) "extended positions" 1 queen placed.
now, doesn't matter how place additional queen. in general case (when placing chess piece), should place on free square (and make sure check all of them). because queens attack do, there should 1 queen in each column, it's sufficient check 8 possible positions each queen. example, in "the next row". or in "the next column" - work too.
Comments
Post a Comment