-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sudoku.java
94 lines (91 loc) · 1.9 KB
/
Sudoku.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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Sudoku {
static void sudoku_solve(int[][] grid) {
if (solve(grid)) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
System.out.print(grid[i][j]+" ");
}
System.out.println();
}
}
}
static boolean solve(int[][] grid) {
Integer x = -1, y = -1;
Map<String,Integer> map= new HashMap<>();
//check for completion
if (isFull(grid,map)){
return true;
}
else{
x=map.get("x");
y=map.get("y");
for (int i = 1; i <= grid.length; i++) {
//check is safe to place a digit in a place
if(isSafe(grid,x,y,i)){
grid[x][y] = i;
if (solve(grid))
return true;
//Reset values for backtracking
grid[x][y] = 0;
}
}
}
return false;
}
//used to check its done if not return values
static boolean isFull(int[][] grid,Map<String,Integer> map) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
if (grid[i][j] == 0) {
map.put("x",i);
map.put("y",j);
return false;
}
}
}
return true;
}
static boolean isSafe(int[][] grid,int x,int y,int j)
{
for(int i=0;i<grid.length;i++)
{
//check horizontal
if(grid[x][i]==j)
return false;
//vertical
if(grid[i][y]==j)
return false;
}
//Find square Block
int xq=x/3*3;
int yq=y/3*3;
for(int a=xq;a<xq+3;a++)
{
for(int b=yq;b<yq+3;b++)
{
if(grid[a][b]==j)
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
int board[][] = new int[9][9];
n = in .nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
board[j][k] = in .nextInt();
}
}
sudoku_solve(board);
}
}
}