forked from CodeSadhu/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku_Solver
77 lines (75 loc) · 2.28 KB
/
Sudoku_Solver
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
import java.util.*;
public class Main {
public static void main(String args[]) {
// Your Code Here
int N = scn.nextInt();
int[][] arr = new int[N][N];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
arr[i][j] = scn.nextInt();
}
}
sudoku(arr,0,0);
}
static Scanner scn = new Scanner(System.in);
public static void sudoku(int[][] arr, int row, int col) {
if (col == arr[0].length) {
row++;
col = 0;
}
if (row == arr.length) {
display(arr);
return;
}
if (arr[row][col] != 0) {
sudoku(arr, row, col + 1);
return;
}
for (int i = 1; i <= 9; i++) {
if (isItSafe(arr, row, col, i)) {
arr[row][col] = i;
sudoku(arr, row, col + 1);
arr[row][col] = 0;
}
}
}
public static boolean isItSafe(int[][] arr, int row, int col, int val) {
return isitsaferow(arr, row, val) && isitsafecol(arr, col, val) && isitsafecolumn(arr, row, col, val);
}
public static boolean isitsaferow(int[][] arr, int row, int i) {
for (int c = 0; c < arr[0].length; c++) {
if (arr[row][c] == i) {
return false;
}
}
return true;
}
public static boolean isitsafecol(int[][] arr, int col, int i) {
for (int r = 0; r < arr.length; r++) {
if (arr[r][col] == i) {
return false;
}
}
return true;
}
public static boolean isitsafecolumn(int[][] arr, int row, int col, int i) {
int bsr = row - row % 3;
int bsc = col - col % 3;
for (int r = bsr; r < bsr + 3; r++) {
for (int c = bsc; c < bsc + 3; c++) {
if (arr[r][c] == i) {
return false;
}
}
}
return true;
}
public static void display(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}