-
Notifications
You must be signed in to change notification settings - Fork 20
/
SudokuSolver.java
143 lines (124 loc) · 3.17 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.techmisal.hard;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
Empty cells are indicated by the character '.'.
Note:
The given board contain only digits 1-9 and the character '.'.
You may assume that the given Sudoku puzzle will have a single unique solution.
The given board size is always 9x9.
Input
53..7....
6..195...
.98....6.
8...6...3
4..8.3..1
7...2...6
.6....28.
...419..5
....8..79
TLE
1....8...
.8.74.1.5
..792..3.
9..1....4
2.......1
4....5..3
.2..548..
8.4.97.1.
...8....6
*/
public class SudokuSolver {
public void solveSudoku(char[][] board) {
solve(board);
}
private boolean solve(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if(board[i][j] == '.') {
for(int value = 49; value <= 57; value++) {
if(isValidValueForCell(board, i, j, (char) value)) {
board[i][j] = (char) value;
if(solve(board)) {
return true;
} else {
board[i][j] = '.';
}
}
}
return false;
}
}
}
return true;
}
private boolean isValidValueForCell(char[][] board, int row, int col, char value) {
return isValidValueForRow(board, row, value) && isValidValueForCol(board, col, value) && isValidValueForSubBox(board, row, col, value);
}
private boolean isValidValueForRow(char[][] board, int row, char value) {
for(int i = 0; i < 9; i++) {
if(board[row][i] == value)
return false;
}
return true;
}
private boolean isValidValueForCol(char[][] board, int col, char value) {
for(int i = 0; i < 9; i++) {
if(board[i][col] == value)
return false;
}
return true;
}
private boolean isValidValueForSubBox(char[][] board, int row, int col, char value) {
int subRow = getSubBoxStartRowCol(row);
int subCol = getSubBoxStartRowCol(col);
for (int i = subRow; i < subRow + 3; i++) {
for (int j = subCol; j < subCol + 3; j++) {
if (board[i][j] == value)
return false;
}
}
return true;
}
private int getSubBoxStartRowCol(int index) {
if (index >= 0 && index <= 2)
return 0;
if (index >= 3 && index <= 5)
return 3;
if (index >= 6 && index <= 8)
return 6;
return 0;
}
private char[][] getSampleBoard() throws IOException {
char[][] board = new char[9][9];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
int row = 0;
while(row < 9 && (line = br.readLine()) != null) {
for(int i = 0; i < 9; i++) {
board[row][i] = line.charAt(i);
}
row++;
}
return board;
}
private void printBoard(char[][] board) {
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException {
SudokuSolver obj = new SudokuSolver();
char[][] board = obj.getSampleBoard();
obj.solveSudoku(board);
obj.printBoard(board);
}
}