UE11
This commit is contained in:
@@ -9,24 +9,60 @@ import java.util.List;
|
||||
public class Sudoku {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/easy.sudoku";
|
||||
int[][] sudoku = readSudoku(sudokuPath);
|
||||
print(sudoku);
|
||||
String sudokuPath;
|
||||
sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/easy.sudoku";
|
||||
readSudoku(sudokuPath);
|
||||
sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/expert.sudoku";
|
||||
readSudoku(sudokuPath);
|
||||
sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/intermedia.sudoku";
|
||||
readSudoku(sudokuPath);
|
||||
sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/simple.sudoku";
|
||||
readSudoku(sudokuPath);
|
||||
}
|
||||
|
||||
public static int[][] readSudoku(String path) {
|
||||
int[][] sudoku = new int[9][9];
|
||||
public static void readSudoku(String path) {
|
||||
try {
|
||||
List<String> input = Files.readAllLines(Path.of(path));
|
||||
|
||||
for (String line : input) {
|
||||
int[][] sudoku = new int[9][9];
|
||||
String[] split = line.split(": ");
|
||||
System.out.println("\n" + split[0] + ":");
|
||||
for (int i = 0; i < split[1].length(); i++)
|
||||
if (!(split[1].charAt(i) == '.'))
|
||||
sudoku[i / 9][i % 9] = Integer.parseInt(String.valueOf(split[1].charAt(i)));
|
||||
solveSudoku(sudoku);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return sudoku;
|
||||
}
|
||||
|
||||
public static void print(int[][] in) {
|
||||
for (int[] i : in) System.out.println(Arrays.toString(i));
|
||||
public static void solveSudoku(int[][] sudoku) {
|
||||
boolean[][] isBlockedRow = new boolean[9][10], isBlockedCol = new boolean[9][10], isBlockedBlock = new boolean[9][10];
|
||||
for (int row = 0; row < sudoku.length; row++)
|
||||
for (int col = 0; col < sudoku[row].length; col++) {
|
||||
int value = sudoku[col][row];
|
||||
if (value != 0)
|
||||
isBlockedRow[row][value] = isBlockedCol[col][value] = isBlockedBlock[3 * (row / 3) + col / 3][value] = true;
|
||||
}
|
||||
solveSudoku(sudoku, 0, isBlockedRow, isBlockedCol, isBlockedBlock);
|
||||
}
|
||||
|
||||
}
|
||||
public static void solveSudoku(int[][] sudoku, int n, boolean[][] isBlockedRow, boolean[][] isBlockedCol, boolean[][] isBlockedBlock) {
|
||||
if (n >= sudoku.length * sudoku[0].length) {
|
||||
for (int[] i : sudoku) System.out.println(Arrays.toString(i));
|
||||
return;
|
||||
}
|
||||
int row = n / 9, col = n % 9, block = 3 * (row / 3) + col / 3;
|
||||
if (sudoku[col][row] == 0) {
|
||||
for (int num = 1; num <= 9; num++) {
|
||||
if (isBlockedRow[row][num] || isBlockedCol[col][num] || isBlockedBlock[block][num]) continue;
|
||||
isBlockedRow[row][num] = isBlockedCol[col][num] = isBlockedBlock[block][num] = true;
|
||||
sudoku[col][row] = num;
|
||||
solveSudoku(sudoku, n + 1, isBlockedRow, isBlockedCol, isBlockedBlock);
|
||||
sudoku[col][row] = 0;
|
||||
isBlockedRow[row][num] = isBlockedCol[col][num] = isBlockedBlock[block][num] = false;
|
||||
}
|
||||
} else solveSudoku(sudoku, n + 1, isBlockedRow, isBlockedCol, isBlockedBlock);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
simple_01: .......3....1..694........11493.5.2.....1..8....6....9.9.4.6.5.2........65..28.1.
|
||||
simple_02: 3..8...7......94.6.27...8..68.....4................689..93.5.2.8..6......5..2.7.4
|
||||
simple_03: ...65.......4.8.2...49.3.8..53.....7.........6..3.....4..8..59.....3...492.7.4..6
|
||||
simple_03: ...65.......4.8.2...49.3.8..53.....7.........6..3.....4..8..59.....3...492.7.4..6
|
||||
|
||||
Reference in New Issue
Block a user