Skip to content

Commit 0584fd8

Browse files
authored
sudoku puzzle
1 parent 76fa211 commit 0584fd8

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

SudokuPuzzle.java

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
public class SudokuPuzzle
2+
{
3+
public boolean isSafe(int[][] b, int r, int c, int n)
4+
{
5+
// the loop takes care of the clash in the row of the grid
6+
for (int d = 0; d < b.length; d++)
7+
{
8+
9+
// if the number that we have inserted is already
10+
// present in that row then return false
11+
if (b[r][d] == n)
12+
{
13+
return false;
14+
}
15+
}
16+
17+
// the loop takes care of the clash in the column of the grid
18+
for (int r1 = 0; r1 < b.length; r1++)
19+
{
20+
21+
// if the number that we have inserted is already
22+
// present in that column then return false
23+
if (b[r1][c] == n)
24+
{
25+
return false;
26+
}
27+
}
28+
29+
// the loop takes care of the clash in the sub-grid that is present in the grid
30+
int sqt = (int)Math.sqrt(b.length);
31+
int boxRowSt = r - r % sqt;
32+
int boxColSt = c - c % sqt;
33+
34+
for (int r1 = boxRowSt; r1 < boxRowSt + sqt; r1++)
35+
{
36+
for (int d = boxColSt; d < boxColSt + sqt; d++)
37+
{
38+
// if the number that we have inserted is already
39+
// present in that sub-grid then return false
40+
if (b[r1][d] == n)
41+
{
42+
return false;
43+
}
44+
}
45+
}
46+
47+
// if there is no clash in the grid, then it is safe and
48+
// true is returned
49+
return true;
50+
}
51+
52+
public boolean solveSudoku(int[][] b, int num)
53+
{
54+
int r = -1;
55+
int c = -1;
56+
boolean isVacant = true;
57+
for (int i = 0; i < num; i++)
58+
{
59+
for (int j = 0; j < num; j++)
60+
{
61+
if (b[i][j] == 0)
62+
{
63+
r = i;
64+
c = j;
65+
66+
// false value means
67+
// there is still some
68+
// vacant cells in the grid
69+
isVacant = false;
70+
break;
71+
}
72+
}
73+
74+
if (!isVacant)
75+
{
76+
break;
77+
}
78+
}
79+
80+
// there is no empty space left
81+
// in the grid
82+
if (isVacant)
83+
{
84+
return true;
85+
}
86+
87+
// otherwise for each row do the backtracking
88+
for (int no = 1; no <= num; no++)
89+
{
90+
if (isSafe(b, r, c, no))
91+
{
92+
b[r][c] = no;
93+
if (solveSudoku(b, num))
94+
{
95+
// display(board, num);
96+
return true;
97+
}
98+
else
99+
{
100+
101+
b[r][c] = 0;
102+
}
103+
}
104+
}
105+
return false;
106+
}
107+
108+
public void display(int[][] b, int n)
109+
{
110+
111+
// We have got the solution, just display it
112+
for (int i = 0; i < n; i++)
113+
{
114+
for (int d = 0; d < n; d++)
115+
{
116+
System.out.print(b[i][d]);
117+
System.out.print(" ");
118+
}
119+
120+
System.out.print("\n");
121+
122+
if ((i + 1) % (int)Math.sqrt(n) == 0)
123+
{
124+
System.out.print("");
125+
}
126+
}
127+
}
128+
129+
// main method
130+
public static void main(String argvs[])
131+
{
132+
133+
// the 9 x 9 grid
134+
int[][] b = new int[][] { { 7, 0, 0, 0, 0, 0, 2, 0, 0 },
135+
{ 4, 0, 2, 0, 0, 0, 0, 0, 3 },
136+
{ 0, 0, 0, 2, 0, 1, 0, 0, 0 },
137+
{ 3, 0, 0, 1, 8, 0, 0, 9, 7 },
138+
{ 0, 0, 9, 0, 7, 0, 6, 0, 0 },
139+
{ 6, 5, 0, 0, 3, 2, 0, 0, 1 },
140+
{ 0, 0, 0, 4, 0, 9, 0, 0, 0 },
141+
{ 5, 0, 0, 0, 0, 0, 1, 0, 6 },
142+
{ 0, 0, 6, 0, 0, 0, 0, 0, 8 }
143+
} ;
144+
145+
// creating an object of the class SudokuPuzzle
146+
SudokuPuzzle obj = new SudokuPuzzle();
147+
148+
// computing the size of the grid
149+
int size = b.length;
150+
151+
System.out.println("The grid is: ");
152+
for(int i = 0; i < size; i++)
153+
{
154+
for(int j = 0; j < size; j++)
155+
{
156+
System.out.print(b[i][j] + " ");
157+
}
158+
159+
System.out.println();
160+
}
161+
System.out.println();
162+
if (obj.solveSudoku(b, size))
163+
{
164+
// display solution
165+
166+
System.out.println("The solution of the grid is: ");
167+
obj.display(b, size);
168+
}
169+
else
170+
{
171+
System.out.println("There is no solution available.");
172+
}
173+
}
174+
}

0 commit comments

Comments
 (0)