-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.java
90 lines (81 loc) · 2.6 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
import java.io.*;
import java.util.*;
public class Solution
{
static int[][] sudoku = new int[9][9] ; //global declaration //
static int R,C;
static void master(int r, int c)
{
int temp;
if( r > 8 ) // starts from cell location(0,0) to end //
display();
else if( sudoku[r][c]!=0 )
move(r,c); //move for non zero NUMBER//
else
{
for(temp=0 ; temp<=9 ; ++temp)
{
if((checkrow(r,temp) == 1) && (checkcolumn(c,temp) == 1 ) && (checkgrid(r,c,temp) ==1 ))
{
sudoku[r][c] = temp; //assigning value to blank location //
move(r,c); // important for continuation of cycle //
}
}
sudoku[r][c] = 0; //if not able to solve // again put zero // --- //
}
}
static void display()
{
for(int i=0; i<9 ; ++i)
{
for(int j=0 ; j<9 ;++j)
System.out.print(sudoku[i][j]);
System.out.println();
}
}
static void move(int r, int c)
{
if(c<8)
master(r,c+1); //important logic // important // careful not " c!<=8 " //
else
master(r+1,0) ;
}
static int checkrow(int r,int num) //row function//
{
for(int c=0 ; c<9 ;c++)
if( sudoku[r][c]== num )
return 0; //if found//
return 1; //if not found//
}
static int checkcolumn(int c,int num) //column function //
{
for(int r=0 ; r<9 ;r++)
if( sudoku[r][c]== num )
return 0; //if found//
return 1; //if not found//
}
static int checkgrid(int r, int c,int num )
{
r = (r/3)*3 ;
c = (c/3)*3 ;
for(R=0 ; R<3 ;++R)
for(C=0 ; C<3 ;C++)
if( sudoku[r+R][c+C] == num)
return 0; //if found//
return 1; //if not found//
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int i,j;
for(i=0;i<9;i++)
{
String s = sc.next();
for(j=0;j<9;j++)
{
sudoku[i][j] = Character.getNumericValue(s.charAt(j));
}
}
master(0,0); //sudoku starts from cell 0,0 ...//
}
}