-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathSudokuSolver.java
More file actions
92 lines (71 loc) · 1.79 KB
/
SudokuSolver.java
File metadata and controls
92 lines (71 loc) · 1.79 KB
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
// import java.io.*;
import java.util.*;
public class SudokuSolver {
public static void display(int[][] board){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
public static void solveSudoku(int[][] board, int i, int j) {
// write yopur code here
if(i==9){
display(board);
return;
}
int nc;
int nr;
if(j==board.length-1){
nc=0;
nr=i+1;
}else{
nr=i;
nc=j+1;
}
if(board[i][j]==0){
for(int val=1;val<=9;val++){
if(isSafe(board,i,j,val)==true){
board[i][j]=val;
solveSudoku(board,nr,nc);
board[i][j]=0;
}
}
}else{
solveSudoku(board,nr,nc);
}
}
public static boolean isSafe(int[][] board,int row,int col,int val){
for(int i=0,j=col;i<board.length;i++){
if(board[i][col]==val){
return false;
}
}
for(int j=0,i=row;j<board.length;j++){
if(board[i][j]==val){
return false;
}
}
int sr=(row/3)*3;
int sc=(col/3)*3;
for(int r=sr;r<=sr+2;r++){
for(int c=sc;c<=sc+2;c++){
if(board[r][c]==val){
return false;
}
}
}
return true;
}
public static void main(String[] args) throws Exception {
Scanner scn = new Scanner(System.in);
int[][] arr = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
arr[i][j] = scn.nextInt();
}
}
solveSudoku(arr, 0, 0);
}
}