Benis
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package arrays;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Queue;
|
||||
import tester.Tester;
|
||||
|
||||
class Sieve implements Iterator<Integer> {
|
||||
@@ -39,27 +39,46 @@ class Sieve implements Iterator<Integer> {
|
||||
|
||||
class Sudoku {
|
||||
static boolean solved(int[][] puz) {
|
||||
// First check the data is shaped correctly.
|
||||
if (puz.length != 9) return false;
|
||||
for (int[] row : puz)
|
||||
if (row.length != 9) return false;
|
||||
|
||||
// Make sure they're all valid integers.
|
||||
for (int[] row : puz)
|
||||
for (int[] row : puz) {
|
||||
if (row.length != 9 || isRepetitive(row))
|
||||
return false; // Check row's the right length & there'ren't any
|
||||
// repeats.
|
||||
for (int n : row)
|
||||
if (n < 1 || n > 9) return false;
|
||||
|
||||
// Check that there'ren't any repeats in a row.
|
||||
for (int[] row : puz) {
|
||||
for (int n : row) {}
|
||||
}
|
||||
|
||||
int[][] transposed = transpose(puz);
|
||||
// Check that there'ren't any repeats in a column.
|
||||
|
||||
// Check that there'ren't any repeats in a 3x3 square.
|
||||
for (int[] col : transposed)
|
||||
if (isRepetitive(col)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Does the given array repeat itself?
|
||||
static boolean isRepetitive(int[] arr) {
|
||||
HashMap<Integer, Boolean> seen = new HashMap<>();
|
||||
|
||||
for (int n : arr)
|
||||
if (seen.containsKey(n)) return true;
|
||||
else seen.put(n, true);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return the transpose of a matrix. Assumes the matrix is properly shaped
|
||||
// and sized.
|
||||
static int[][] transpose(int[][] m) {
|
||||
int[][] transposed = new int[9][9];
|
||||
|
||||
for (int i = 0; i < 9; i++)
|
||||
for (int j = 0; j < 9; j++) transposed[j][i] = m[i][j];
|
||||
|
||||
return transposed;
|
||||
}
|
||||
}
|
||||
|
||||
class Examples {
|
||||
@@ -99,12 +118,24 @@ class Examples {
|
||||
{7, 1, 3, 9, 2, 4, 8, 5, 6}, {9, 6, 1, 5, 3, 7, 2, 8, 4},
|
||||
{2, 8, 7, 4, 1, 9, 6, 3, 5}, {3, 4, 5, 2, 8, 6, 1, 7, 9}
|
||||
};
|
||||
int[][] testPuz6 = {
|
||||
{5, 3, 4, 6, 7, 8, 9, 1, 2},
|
||||
{5, 7, 2, 1, 9, 6, 3, 4, 8}, // Two 5's in the same column.
|
||||
{1, 9, 8, 3, 4, 2, 5, 6, 7},
|
||||
{8, 5, 9, 7, 6, 1, 4, 2, 3},
|
||||
{4, 2, 6, 8, 5, 3, 7, 9, 1},
|
||||
{7, 1, 3, 9, 2, 4, 8, 5, 6},
|
||||
{9, 6, 1, 5, 3, 7, 2, 8, 4},
|
||||
{2, 8, 7, 4, 1, 9, 6, 3, 5},
|
||||
{3, 4, 5, 2, 8, 6, 1, 7, 9}
|
||||
};
|
||||
|
||||
t.checkExpect(Sudoku.solved(testPuz1), true);
|
||||
t.checkExpect(Sudoku.solved(testPuz2), false);
|
||||
t.checkExpect(Sudoku.solved(testPuz3), false);
|
||||
t.checkExpect(Sudoku.solved(testPuz4), false);
|
||||
t.checkExpect(Sudoku.solved(testPuz5), false);
|
||||
t.checkExpect(Sudoku.solved(testPuz6), false);
|
||||
}
|
||||
void testSieve(Tester t) {
|
||||
Sieve sieve = new Sieve(29);
|
||||
|
Reference in New Issue
Block a user