forked from imnishant/GeeksforGeeks-Java-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolveSudoku.java
More file actions
78 lines (75 loc) · 2.02 KB
/
SolveSudoku.java
File metadata and controls
78 lines (75 loc) · 2.02 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
import java.util.*;
import java.io.*;
class DataStructure
{
static boolean check(int mat[][], int i, int j, int num)
{
for(int t=0 ; t<9 ; t++)
{
if(mat[i][t] == num || mat[t][j] == num)
return false;
}
int row = i - i % 3;
int col = j - j % 3;
for(int m=0 ; m<3 ; m++)
for(int n=0 ; n<3 ; n++)
if(mat[m+row][n+col] == num)
return false;
return true;
}
static boolean solve(int mat[][], Stack<ArrayList<Integer>> st)
{
if(st.size() == 0)
return true;
ArrayList<Integer> al = st.pop();
int i = al.get(0);
int j = al.get(1);
int num = 1;
while(num < 10)
{
if(check(mat, i, j, num))
{
mat[i][j] = num;
if(solve(mat, st))
return true;
mat[i][j] = 0;
}
num++;
}
st.push(al);
return false;
}
public static void main (String[] args) throws IOException
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
while(t-- > 0)
{
Stack<ArrayList<Integer>> st = new Stack<ArrayList<Integer>>();
String s[] = bf.readLine().trim().split(" ");
int mat[][] = new int[9][9];
for(int i=0 ; i<9 ; i++)
{
for(int j=0 ; j<9 ; j++)
{
mat[i][j] = Integer.parseInt(s[i*9+j]);
if(mat[i][j] == 0)
{
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(i);
al.add(j);
st.push(al);
}
}
}
if(!solve(mat, st))
System.exit(0);
for(int i=0 ; i<9 ; i++)
{
for(int j=0 ; j<9 ; j++)
System.out.print(mat[i][j] + " ");
System.out.println();
}
}
}
}