Skip to content

Commit 937c8bd

Browse files
authored
Merge pull request sanscript-tech#330 from Sapna2001/sudoku
Sudoku solver in Java
2 parents c7fea47 + 0ba0f47 commit 937c8bd

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

Java/Sudoku_Solver/Readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Sudoku Solver
2+
This is a program to solve a sudoku. Instead of the blank spaces, 0 has to be inputed.
3+
4+
## Sample Input
5+
![input](https://user-images.githubusercontent.com/56690856/98670547-d74b0e00-2378-11eb-9fe2-44e124d1cef5.png)
6+
7+
## Sample Output
8+
![output](https://user-images.githubusercontent.com/56690856/98670752-2bee8900-2379-11eb-837f-1b613c5137ca.png)
9+
10+
## Demo
11+
![demo](https://user-images.githubusercontent.com/56690856/98670816-49235780-2379-11eb-9245-2d6b6aa02ef8.png)
12+
13+
## Time Complexity
14+
O(9^(n*n))
15+
16+
## Space Complexity
17+
O(n*n)
18+

Java/Sudoku_Solver/SudokuSolver.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package sudoku;
2+
3+
import java.util.*;
4+
5+
public class SudokuSolver {
6+
7+
// N=9 as there will be 9 rows and 9 colums
8+
static int N = 9;
9+
10+
static boolean solveSudoku(int grid[][], int row, int col) {
11+
// if we cross the last cell then we return true to avoid further back-tracking
12+
if (row == N - 1 && col == N)
13+
return true;
14+
// if we reach the last column then we move to the next row after setting col as 0
15+
if (col == N)
16+
{
17+
row++;
18+
col = 0;
19+
}
20+
// check whether the cell is 0 and if it is not 0 then we move to the next column for further processing
21+
if (grid[row][col] != 0)
22+
return solveSudoku(grid, row, col + 1);
23+
24+
// check if a no from 1 to 0=9 can be placed in a given row and column
25+
for(int no=1;no<10;no++) {
26+
if (isSafe(grid,row,col,no)) {
27+
// assigning no to the cell assuming it be correct
28+
grid[row][col] = no;
29+
30+
// then go for the next column assuming the no assigned is correct
31+
if (solveSudoku(grid,row,col+1))
32+
return true;
33+
}
34+
// if the assumption is wrong assign 0 to the cell
35+
grid[row][col] = 0;
36+
}
37+
return false;
38+
}
39+
40+
static boolean isSafe(int grid[][], int row, int col,int num) {
41+
// return false if the given num is found in the column
42+
for (int i=0;i<8;i++) {
43+
if (grid[i][col]==num)
44+
return false;
45+
}
46+
47+
// return false if the given num is found in the row
48+
for (int i=0;i<8;i++) {
49+
if (grid[row][i]==num)
50+
return false;
51+
}
52+
// check if num is present in the 3*3 matrix
53+
int startRow = row -(row%3);
54+
int startColumn = col -(col%3);
55+
for(int i=0;i<3;i++) {
56+
for(int j=0;j<3;j++) {
57+
if (grid[i+startRow][j+startColumn]==num)
58+
return false;
59+
}
60+
}
61+
return true;
62+
}
63+
64+
// display the solved sudoku
65+
static void print(int[][] grid) {
66+
System.out.println("Solved Sudoku");
67+
for(int i=0;i<N;i++) {
68+
for(int j=0;j<N;j++)
69+
System.out.print(grid[i][j]+" ");
70+
System.out.println();
71+
}
72+
}
73+
74+
public static void main(String[] args) {
75+
// TODO Auto-generated method stub
76+
Scanner sc = new Scanner(System.in);
77+
int[][] grid = new int[N][N];
78+
System.out.println("Enter the Sudoku to be solved");
79+
// enter the matrix
80+
for(int i=0; i<N;i++) {
81+
for(int j=0;j<N;j++) {
82+
grid[i][j]=sc.nextInt();
83+
}
84+
}
85+
// check if solution exists
86+
if(solveSudoku(grid,0,0))
87+
print(grid);
88+
else
89+
System.out.println("Solution does not exist");
90+
}
91+
92+
}

0 commit comments

Comments
 (0)