我正在研究 n-queen 回溯器。有人可以向我解释如何other_row_pos
检查对角线吗?我不确定它为什么起作用或如何起作用。
取自维基书籍 - http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/N-Queens:
bool isSafe(int queen_number, int row_position) {
// Check each queen before this one
for(int i=0; i<queen_number; i++) {
// Get another queen's row_position
int other_row_pos = position[i];
// Now check if they're in the same row or diagonals
if(other_row_pos == row_position || // Same row
other_row_pos == row_position - (queen_number-i) || // Same diagonal
other_row_pos == row_position + (queen_number-i)) // Same diagonal
return false;
}
return true;
}