halshadshafds

This commit is contained in:
Jacob Signorovitch
2025-05-28 08:44:53 -04:00
parent 473a3f7cb8
commit 31e83a33fb
2 changed files with 22 additions and 10 deletions

View File

@@ -57,15 +57,13 @@ class Sudoku {
for (int[] col : transposed)
if (isRepetitive(col)) return false;
/*
// Check there'ren't any repeats in a square.
for (int i = 0; i < 9; i++) {
int[] square = new int[3][3];
for (int j = i; j < i + 3; j++) {
// for (int k = i; k < i + 3; k++) { square[i][k] }
}
}
*/
// Check all 3x3 squares.
for (int squareRow = 0; squareRow < 3; squareRow++) {
for (int squareCol = 0; squareCol < 3; squareCol++) {
if (squareCheck(puz, squareRow * 3, squareCol * 3))
return false;
}
}
return true;
}
@@ -91,6 +89,20 @@ class Sudoku {
return transposed;
}
// Check if a 3x3 square repeats values.
static boolean squareCheck(int[][] puz, int startRow, int startCol) {
HashMap<Integer, Boolean> seen = new HashMap<>();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
int val = puz[startRow + i][startCol + j];
if (seen.containsKey(val)) return true;
else seen.put(val, true);
}
return false;
}
}
class Examples {
@@ -166,6 +178,7 @@ class Examples {
t.checkExpect(Sudoku.solved(testPuz6), false);
t.checkExpect(Sudoku.solved(testPuz7), false);
}
void testSieve(Tester t) {
Sieve sieve = new Sieve(29);
t.checkExpect(sieve.hasNext(), true);