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">
|
<profile version="1.0">
|
||||||
<option name="myName" value="Project Default" />
|
<option name="myName" value="Project Default" />
|
||||||
<inspection_tool class="CommentedOutCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
<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>
|
||||||
<inspection_tool class="RegExpAnonymousGroup" enabled="true" level="WARNING" enabled_by_default="true" />
|
<inspection_tool class="RegExpAnonymousGroup" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
<inspection_tool class="RegExpEscapedMetaCharacter" enabled="false" level="INFORMATION" enabled_by_default="false" />
|
<inspection_tool class="RegExpEscapedMetaCharacter" enabled="false" level="INFORMATION" enabled_by_default="false" />
|
||||||
|
|||||||
@@ -9,24 +9,60 @@ import java.util.List;
|
|||||||
public class Sudoku {
|
public class Sudoku {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/easy.sudoku";
|
String sudokuPath;
|
||||||
int[][] sudoku = readSudoku(sudokuPath);
|
sudokuPath = "src/UE11_140125_Rekursion4/Sudokus/easy.sudoku";
|
||||||
print(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) {
|
public static void readSudoku(String path) {
|
||||||
int[][] sudoku = new int[9][9];
|
|
||||||
try {
|
try {
|
||||||
List<String> input = Files.readAllLines(Path.of(path));
|
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) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
return sudoku;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void print(int[][] in) {
|
public static void solveSudoku(int[][] sudoku) {
|
||||||
for (int[] i : in) System.out.println(Arrays.toString(i));
|
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_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_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