Skip to content

Commit db9980d

Browse files
committed
Fixed operator formatting
1 parent 3c5d1bd commit db9980d

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

src/main/java/com/thealgorithms/backtracking/NQueens.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@
4242
public final class NQueens {
4343

4444
// Store occupied rows for constant time safety check
45-
private static final Set<Integer> OCCUPIED_ROWS = new HashSet<>();
45+
private static final Set<Integer> OCROWS = new HashSet<>();
4646

4747
// Store occupied main diagonals (row - column)
48-
private static final Set<Integer> OCCUPIED_DIAGONALS = new HashSet<>();
48+
private static final Set<Integer> OCDIAG = new HashSet<>();
4949

5050
// Store occupied anti-diagonals (row + columns)
51-
private static final Set<Integer> OCCUPIED_ANTI_DIAGONALS = new HashSet<>();
51+
private static final Set<Integer> OCANTIDIAG = new HashSet<>();
5252

5353
private NQueens() {
5454
}
@@ -101,24 +101,27 @@ private static void getSolution(int boardSize, List<List<String>> solutions, int
101101
columns[columnIndex] = rowIndex;
102102

103103
// Skip current position if row or diagonal is already occupied
104-
if (OCCUPIED_ROWS.contains(rowIndex) || OCCUPIED_DIAGONALS.contains(rowIndex - columnIndex) ||
105-
OCCUPIED_ANTI_DIAGONALS.contains(rowIndex + columnIndex)) {
104+
boolean isROp = OCROWS.contains(rowIndex);
105+
106+
boolean isDOp = OCDIAG.contains(rowIndex - columnIndex) || OCANTIDIAG.contains(rowIndex + columnIndex);
107+
108+
if (isROp || isDOp) {
106109
continue;
107110
}
108111

109112
// Mark current row and diagonal as occupied
110-
OCCUPIED_ROWS.add(rowIndex);
111-
OCCUPIED_DIAGONALS.add(rowIndex - columnIndex);
112-
OCCUPIED_ANTI_DIAGONALS.add(rowIndex + columnIndex);
113+
OCROWS.add(rowIndex);
114+
OCDIAG.add(rowIndex - columnIndex);
115+
OCANTIDIAG.add(rowIndex + columnIndex);
113116

114117
// Move to the next column after placing current queen
115118
getSolution(boardSize, solutions, columns, columnIndex + 1);
116119

117120
// Backtrack by removing current queen
118121

119-
OCCUPIED_ROWS.remove(rowIndex);
120-
OCCUPIED_DIAGONALS.remove(rowIndex - columnIndex);
121-
OCCUPIED_ANTI_DIAGONALS.remove(rowIndex + columnIndex);
122+
OCROWS.remove(rowIndex);
123+
OCDIAG.remove(rowIndex - columnIndex);
124+
OCANTIDIAG.remove(rowIndex + columnIndex);
122125
}
123126
}
124127

0 commit comments

Comments
 (0)