| 11 | * a time. We know this row is empty. |
| 12 | */ |
| 13 | public static boolean checkValid(Integer[] columns, int row1, int column1) { |
| 14 | for (int row2 = 0; row2 < row1; row2++) { |
| 15 | int column2 = columns[row2]; |
| 16 | /* Check if (row2, column2) invalidates (row1, column1) as a queen spot. */ |
| 17 | |
| 18 | /* Check if rows have a queen in the same column */ |
| 19 | if (column1 == column2) { |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | /* Check diagonals: if the distance between the columns equals the distance |
| 24 | * between the rows, then they're in the same diagonal. |
| 25 | */ |
| 26 | int columnDistance = Math.abs(column2 - column1); |
| 27 | int rowDistance = row1 - row2; // row1 > row2, so no need to use absolute value |
| 28 | if (columnDistance == rowDistance) { |
| 29 | return false; |
| 30 | } |
| 31 | } |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | public static void placeQueens(int row, Integer[] columns, ArrayList<Integer[]> results) { |
| 36 | if (row == GRID_SIZE) { // Found valid placement |