-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuduko.java
More file actions
84 lines (75 loc) · 2.22 KB
/
suduko.java
File metadata and controls
84 lines (75 loc) · 2.22 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
package BackTracking;
public class suduko {
static int n=9;
static boolean check(int grid[][],int row,int col){
if(row==n-1 && col==n)
return true;
if(col==n)
{
row++;
col=0;
}
if(grid[row][col]!=0)
return check(grid, row, col+1);
for(int i=1;i<=n;i++){
if(issafe(grid,row,col,i)){
grid[row][col]=i;
// System.out.println(i);
if(check(grid, row, col+1))
return true;
}
grid[row][col]=0;
}
return false;
}
static boolean issafe(int grid[][],int row,int col,int i){
for(int j=0;j<n;j++)
if(grid[row][j]==i)
return false;
for(int j=0;j<n;j++){
if(grid[j][col]==i)
return false;
}
int st=row-row%3,sp=col-col%3;
for(int j=0;j<3;j++){
for(int k=0;k<3;k++){
if(grid[j+st][k+sp]==i)
return false;
}
}
return true;
}
static void print(int grid[][]){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(grid[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args) {
int grid[][]={ { 3, 0, 6, 5, 0, 8, 4, 0, 0 },
{ 5, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 8, 7, 0, 0, 0, 0, 3, 1 },
{ 0, 0, 3, 0, 1, 0, 0, 8, 0 },
{ 9, 0, 0, 8, 6, 3, 0, 0, 5 },
{ 0, 5, 0, 0, 9, 0, 6, 0, 0 },
{ 1, 3, 0, 0, 0, 0, 2, 5, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 7, 4 },
{ 0, 0, 5, 2, 0, 6, 3, 0, 0 } };
if(check(grid,0,0))
print(grid);
else
System.out.println("empty");
}
}
// output
// 3 1 6 5 7 8 4 9 2
// 5 2 9 1 3 4 7 6 8
// 4 8 7 6 2 9 5 3 1
// 2 6 3 4 1 5 9 8 7
// 9 7 4 8 6 3 1 2 5
// 8 5 1 7 9 2 6 4 3
// 1 3 8 9 4 7 2 5 6
// 6 9 2 3 5 1 8 7 4
// 7 4 5 2 8 6 3 1 9