×

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that every row, every column, and every sub-grid of size 3 x 3 has all of the numbers from 1 to 9.

Sudoku can be implemented by various methods in Java.

Simple Approach

To fill the vacant cells, the naive way is to produce all possible combinations of numbers from 1 to 9. Try each configuration one at a time until the right one is discovered, i.e., assign a number between 1 and 9 to each vacant place. Verify that the matrix is secure after filling out every open place.

Algorithm

  • Make a function that determines whether the provided matrix is a valid sudoku puzzle. Maintain a HashMap for the boxes, columns, and rows. If any HashMap number has a frequency greater than 1, return false; otherwise, return true.
  • It is necessary to develop a recursive function that takes a grid and the current row and column indexes.
  • Start several base instances. Display the grid and indicate whether it is safe to do so; if it is unsafe, return false. If the index is at the end of the matrix, that is, if i1=M-1 and j1=M, return false. The second fundamental situation is when you want to move to the following row, i1++, and j1 = 0, and the value of the column is M, i.e., j = M. In the event that the current index is not assigned, fill in the elements from 1 to 9 and then repeat for all 9 cases using the subsequent element's index, which is i1, j1+1. Break the loop and return true if the recursive call returns true.
  • Call the recursive function using the index of the following element, i1, j1+1, if the current index has been assigned.

Program of Sudoku in Java by Simple Approach

Sudoko_example1.java

public class Sudoko_example1{
static int M = 9 ;
static boolean solve_sudoku ( int grid [ ] [ ] , int r ,int c)
{
if ( r == M - 1 && c == M )
return true ;
if (c == M) {
r++ ;
c = 0 ;
}
if ( grid [ r ] [ c ] ! = 0 )
return solve_sudoku ( grid , r , c + 1 ) ;
for  ( int number = 1 ; number  < 10 ; number++  ) {
if  ( is_safe ( grid ,  r ,  c , number ) ) {
grid [ r ] [ c ] = number ;
if ( solve_sudoku ( grid ,  r ,  c + 1 ) )
return true ;
}
grid  [ r ] [ c ] = 0 ;
}
return false ;
}
static void print ( int [ ] [ ] grid )
{
for ( int i1 = 0 ; i1 < M ; i1++ ) {
for ( int j1 = 0 ; j1 < M ; j1++ )
System . out . print ( grid [ i1] [j1 ] + " " ) ;
System . out . println ( ) ;
}
}
static boolean is_safe ( int [ ] [ ] grid ,  int r , int c, int number )
{
for ( int x1 = 0 ; x1 <= 8 ; x1++ )
if ( grid [ r ] [ x1 ] == number )
return false ;
for ( int x1 = 0 ;  x1 < = 8; x1++ )
if ( grid [ x1 ] [ c ] == number )
return false ;
int sr = r - r % 3 , sc = c - c % 3 ;
for  ( int i1 = 0 ;  i1 < 3 ; i1++ )
for ( int j1 = 0 ; j1 < 3 ; j1++ )
if ( grid [i1 + sr ] [ j1 + sc ] == number )
return false ;
return true ;
}
public static void main ( String [ ] args )
{
int grid1 [ ] [ ] = {               { 7 , 0 , 0 , 6 , 0 , 0 , 5 , 0 , 0 } ,   
                                        { 0, 0, 0, 0, 0, 3, 0, 8, 5 },   
                                        { 0, 0, 1, 0, 2, 0, 0, 0, 0 },   
                                        { 0, 0, 0, 5, 0, 7, 0, 0, 0 },   
                                        { 0, 0, 4, 0, 7, 0, 6, 0, 0 },   
                                        { 1, 4, 0, 0, 5, 2, 0, 0, 7 },   
                                        { 0, 0, 0, 4, 0, 9, 0, 0, 0 },   
                                        { 5, 0, 0, 0, 0, 0, 1, 0, 6 },   
                                        { 0 , 0 , 6 , 0 , 0 , 0 , 0 ,  0 , 1 }  } ;
if ( solve_sudoku (grid1 ,  0 , 0 ) )
print ( grid1 ) ;
else
System . out . println  ( " No Solution exists " ) ;
}
}

Output

Sudoku in Java

Backtracking Approach

In this method, we fill each empty cell with a different integer. Prior to assigning a number, we make sure that it is either present in the current column, row, or 3 x 3 sub-grid. We take another number and check its security if the identical number is present.

If the same number is absent, it is assigned, and we then iteratively determine whether or not this assignment results in the answer. We take another number and continue the process if the assignment does not lead to the solution. False is returned and the message "the solution did not exit" is displayed if none of the integers between 1 and 9 find a solution.

Algorithm

  • When a number is allocated to the current index, create a function whose job it is to determine whether or not the grid is secure. Make a hashmap for the boxes, column, and row to store the frequency of the numbers. Return false if the HashMap reveals the frequency of any number higher than 1, and true otherwise. Loops can be used as an alternative to hash maps.
  • You should create a recursive function that takes the grid as an input.
  • Search the grid for the unassigned spot. If the unassigned location is present, then choose a number between 1 and 9, determine whether the number chosen for the current index renders the grid safe, Recursively call the procedure for all of the safe scenarios from 1 to 9 if the grid is safe. Returning true will end the loop if any of the recursive calls return true. Return false if there isn't a recursive call that does so.
  • Return true if there are zero total unassigned places in the grid.

Program of Sudoku in Java by Backtracking Approach

sudoko_back_tracking_example.java

public class sudoko_back_tracking_example
{  
public boolean is_safe ( int [ ] [ ] b1 , int row , int col , int N )  
{  
for (int d1 = 0 ; d1 < b1 . length ; d1++ )  
{  
if ( b1 [ row ] [ d1 ] == N )   
{  
return false ;  
}  
}  


for ( int row2 = 0 ; row2  <  b1 . length ; row2 ++ )  
{  
if  ( b1 [ row2 ] [ col ] == N )  
{  
return false ;  
}  
}  
int s1 = ( int ) Math . sqrt ( b1 . length ) ;  
int brs = row – row  %  s1 ;  
int bcs = col - col  % s1 ;  


for ( int row1 = brs ; row1 < brs  +  s1 ; row1++ )  
{  
for  ( int d1 = bcs ; d1 < bcs + s1 ; d1++ )  
{  
if ( b1 [ row1 ] [ d1 ] == N )  
{  
return false ;  
}  
}  
}  
return true ;  
}  


public boolean solve_sudoku( int [ ] [ ] b1 , int number )  
{  
int row =  -1 ;  
int col =  -1 ;  
boolean is_vacant  = true ;  
for ( int i1 = 0 ; i1 < number ; i1++ )  
{  
for (int j1 = 0 ; j1 < number ; j1++ )  
{  
if ( b1 [ i1 ] [ j1 ] == 0 )  
{  
row = i1;  
col = j1;  
is_vacant = false ;  
break ;  
}  
}  


if ( !is_vacant )   
{  
break ;  
}  
}  
if ( is_vacant )  
{  
return true ;  
}  


for ( int n1 = 1 ; n1 < = number ;  n1++ )  
{  
if ( is_safe ( b1 , row , col , n1 ) )  
{  
b1 [ row ] [ col ] = n1;  
if ( solve_sudoku ( b1 , number ) )  
{  
return true ;  
}  
else  
{  
b1[ row ] [ col ] = 0 ;  
}  
}  
}  
return false ;  
}  


public void display ( int [ ] [ ] b1 , int N )  
{  


for ( int i1 = 0 ; i1 < N ; i1++ )  
{  
for ( int d1 = 0 ; d1 < N ; d1++ )  
{  
System . out . print ( b1 [ i1 ] [ d1 ] ) ;  
System . out . print ( " " ) ;  
}  


System . out . print ( " \n " ) ;  


if  ( ( i1 + 1 ) % ( int ) Math . sqrt ( N ) == 0 )  
{  
System . out . print ( " " ) ;  
}  
}  
}  


public static void main ( String argvs [ ] )  
{  


int  [ ] [ ] b1 = new int [ ] [ ] {               
                { 7 , 0 , 0 , 6 , 0 , 0 , 5 , 0 , 0 },   
                { 0 , 0 , 0 , 0 , 0 , 3 , 0 , 8 , 5 },   
                { 0 , 0 , 1 , 0 , 2 , 0 , 0 , 0 , 0 },   
                { 0, 0 , 0 , 5 , 0 , 7 , 0 , 0 , 0 },   
                { 0 , 0 , 4 , 0, 7 , 0 , 6 , 0 , 0 },   
                { 1 , 4 , 0 , 0 , 5 , 2 , 0 , 0 , 7 } ,   
                { 0 , 0 , 0 , 4 , 0 , 9 , 0 , 0 , 0 } ,   
                { 5 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 6 } ,   
                 { 0 , 0 , 6 , 0 , 0 , 0 , 0 , 0 , 1 }   
                        } ;  


sudoko_back_tracking_example object1 = new sudoko_back_tracking_example();  


int s1 = b1.length;  


System.out.println("The grid is: ");  
for(int i1 = 0; i1 < s1; i1++)  
{  
for(int j1 = 0; j1 < s1; j1++)  
{  
System.out.print(b1[i1][j1] + "");    
}  


System.out.println();  
}  
System.out.println();  
if (object1.solve_sudoku(b1, s1))  
{  


System.out.println("The solution is: ");  
object1.display(b1, s1);  
}  
else   
{  
System.out.println("There is no solution present.");  
}  
}  
}  

Output

Sudoku in Java

Bit Masks Approach

This approach is a small improvement over the previous two approaches. Create a bitmask for each row, column, and box, and set the bit at position "value" to 1 for each element in the grid to enable O(1) tests.

Algorithm

  • Create 3 N-sized arrays (one for rows, columns, and boxes).
  • The boxes are numbered 0 through 8. (We use the following method to determine an element's box-index: row / 3 * 3 + column / 3).
  • Map the grid's initial values first.
  • When an element is added or removed from the grid, the bit is set to 1/0 in the relevant bitmasks.

Program of Sudoku in Java by Bit Masks Approach

sudoko_example3.java

import java.io.*;


class sudoko_example3 {
static int M = 9;


static int r[] = new int[M] , c[] = new int[M], b[] = new int[M];
static Boolean set = false;


static int get_box(int i1,int j1)
{
return i1 / 3 * 3 + j1 / 3;
}




static Boolean is_safe(int i1,int j1,int num)
{
return ((r[i1] >> num) & 1) == 0
&& ((c[j1] >> num) & 1) == 0
&& ((b[get_box(i1,j1)] >> num) & 1) == 0;
}




static void set_initial_values(int grid[][])
{
for (int i1 = 0; i1 < grid.length;i1++){
for (int j1 = 0; j1 < grid.length; j1++){
r[i1] |= 1 << grid[i1][j1];
c[j1] |= 1 << grid[i1][j1];
b[get_box(i1, j1)] |= 1 << grid[i1][j1];
}
}
}




static Boolean solve_sudoku(int grid[][] ,int i1, int j1)
{
if(!set){
set = true;
set_initial_values(grid);
}


if(i1 == M - 1 && j1 == M)
return true;
if(j1 == M){
j1 = 0;
i1++;
}


if(grid[i1][j1]>0)
return solve_sudoku(grid, i1, j1 + 1);


for (int nr1 = 1; nr1 <= M;nr1++)
{
if(is_safe(i1, j1, nr1))
{


grid[i1][j1] = nr1;
r[i1] |= 1 << nr1;
c[j1] |= 1 << nr1;
b[get_box(i1, j1)] |= 1 << nr1;


if(solve_sudoku(grid, i1,j1 + 1))
return true;




r[i1] &= ~(1 << nr1);
c[j1] &= ~(1 << nr1);
b[get_box(i1, j1)] &= ~(1 << nr1);
}


grid[i1][j1] = 0;
}


return false;
}


static void print(int grid[][])
{
for (int i1 = 0; i1 < grid.length; i1++){
for (int j1 = 0; j1 < grid[0].length; j1++){
System.out.printf("%d ",grid[i1][j1]);
}
System.out.println();
}
}




public static void main(String args[])
{
int grid[][] = {                    { 7, 0, 0, 6, 0, 0, 5, 0, 0 },   
                                        { 0, 0, 0, 0, 0, 3, 0, 8, 5 },   
                                        { 0, 0, 1, 0, 2, 0, 0, 0, 0 },   
                                        { 0, 0, 0, 5, 0, 7, 0, 0, 0 },   
                                        { 0, 0, 4, 0, 7, 0, 6, 0, 0 },   
                                        { 1, 4, 0, 0, 5, 2, 0, 0, 7 },   
                                        { 0, 0, 0, 4, 0, 9, 0, 0, 0 },   
                                        { 5, 0, 0, 0, 0, 0, 1, 0, 6 },   
                                        { 0, 0, 6, 0, 0, 0, 0, 0, 1 }};


if (solve_sudoku(grid,0 ,0))
print(grid);
else
System.out.println ( " No solution exists " ) ;
}
}

Output

Sudoku in Java

Related Topics

Java exception list

Java uses exceptions, like the majority of contemporary programming languages, to deal with both errors and "extraordinary events." When an exception arises inside the program, it messes up the regular...

6 minutes read.

How to Convert Date to Timestamp in Java

How to Convert Date to Timestamp in Java You can convert Date to Timestamp by using the getTime() method of Date class. It returns the long millisecond from Epoch which can...

1 minute read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Java Math copySign() Method

The copySign() method of Math class returns the first floating-point argument with the sign of the second argument. Syntax: public static float copySign(float magnitude, float sign)public static double copySign(double magnitude, double sign) Parameters: The...

1 minute read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.