-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudokuSolver.java
94 lines (80 loc) · 2.75 KB
/
sudokuSolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package Backtracking;
import java.util.Scanner;
public class sudokuSolver {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int N = input.nextInt();
int SIZE = N * N;
int CELLS = N * N * N * N;
int[] puzzle = new int[CELLS];
for (int i = 0; i < CELLS; i++) {
puzzle[i] = input.nextInt();
}
if (solve(puzzle)) {
System.out.println("Puzzle Solved, here is the solution: ");
for (int i = 0; i < CELLS; i++) {
System.out.print(" " + puzzle[i]);
if ((i + 1) % N == 0) System.out.print(" |");
if (i != 0 && (i + 1) % SIZE == 0) System.out.println();
if ((i + 1) % (N*SIZE) == 0) System.out.println("----------------------");
}
} else {
System.out.println("Could not solve this puzzle");
}
System.out.println();
input.close();
}
public static boolean solve(int[] puzzle) {
int N = (int) Math.round(Math.pow(puzzle.length, 0.25d)); // length ^ 0.25
int SIZE = N * N;
int CELLS = SIZE * SIZE;
boolean noEmptyCells = true;
int myRow = 0, myCol = 0;
for (int i = 0; i < CELLS; i++) {
if (puzzle[i] == 0) {
myRow = i / SIZE;
myCol = i % SIZE;
noEmptyCells = false;
break;
}
}
if (noEmptyCells) return true;
for (int choice = 1; choice <= SIZE; choice++) {
boolean isValid = true;
int gridRow = myRow / N;
int gridCol = myCol / N;
// check grid for duplicates
for (int row = N * gridRow; row < N * gridRow + N; row++)
for (int col = N * gridCol; col < N * gridCol + N; col++)
if (puzzle[row * SIZE + col] == choice)
isValid = false;
// row & column
for (int j = 0; j < SIZE; j++)
if (puzzle[SIZE * j + myCol] == choice || puzzle[myRow * SIZE + j] == choice) {
isValid = false;
break;
}
if (isValid) {
puzzle[myRow * SIZE + myCol] = choice;
boolean solved = solve(puzzle);
if (solved) return true;
else puzzle[myRow * SIZE + myCol] = 0;
}
}
return false;
}
}
/*
Sample Input:
-------------
3
4 0 0 0 0 0 0 0 5
0 0 9 4 0 2 8 0 0
0 6 0 0 5 0 0 9 0
0 3 0 0 8 0 0 2 0
0 0 2 5 0 1 3 0 0
0 9 0 0 4 0 0 7 0
0 1 0 0 6 0 0 5 0
0 0 8 1 0 5 9 0 0
5 0 0 0 0 0 0 0 7
*/