UE11
This commit is contained in:
2
.idea/inspectionProfiles/Project_Default.xml
generated
2
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -2,7 +2,7 @@
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="CommentedOutCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||
<option name="minLines" value="10" />
|
||||
<option name="minLines" value="11" />
|
||||
</inspection_tool>
|
||||
<inspection_tool class="RegExpAnonymousGroup" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
<inspection_tool class="RegExpEscapedMetaCharacter" enabled="false" level="INFORMATION" enabled_by_default="false" />
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user