Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sorry last one was typed incorrect. Here I have a code that displays and gives solution so how can I make it so I am

Sorry last one was typed incorrect. Here I have a code that displays and gives solution so how can I make it so I am righting my answer but if its wrong it says incorrect and if I am right it says correct will still showing the solution?
public class SudokuPuzzle{
public boolean isSafe(int[][] b, int r, int c, int n)
{
for (int d =0; d < b.length; d++)
{
if (b[r][d]== n)
{
return false;
}
}
for (int r1=0; r1< b.length; r1++)
{
if (b[r1][c]== n)
{
return false;
}
}
int sqt =(int)Math.sqrt(b.length);
int boxRowSt = r - r % sqt;
int boxColSt = c - c % sqt;
for (int r1= boxRowSt; r1< boxRowSt + sqt; r1++)
{
for (int d = boxColSt; d < boxColSt + sqt; d++)
{
if (b[r1][d]== n)
{
return false;
}
}
}
return true;
}
public boolean solveSudoku(int[][] b, int num)
{
int r =-1;
int c =-1;
boolean isVacant = true;
for (int i =0; i < num; i++)
{
for (int j =0; j < num; j++)
{
if (b[i][j]==0)
{
r = i;
c = j;
isVacant = false;
break;
}
}
if (!isVacant)
{
break;
}
}
if (isVacant)
{
return true;
}
for (int no =1; no <= num; no++)
{
if (isSafe(b, r, c, no))
{
b[r][c]= no;
if (solveSudoku(b, num))
{
return true;
}
else
{
b[r][c]=0;
}
}
}
return false;
}
public void display(int[][] b, int n)
{
for (int i =0; i < n; i++)
{
for (int d =0; d < n; d++)
{
System.out.print(b[i][d]);
System.out.print("");
}
System.out.print("
");
if ((i +1)%(int)Math.sqrt(n)==0)
{
System.out.print("");
}
}
}
// main method
public static void main(String argvs[])
{
// the 9 x 9 grid
int[][] b = new int[][]{{7,0,0,0,0,0,2,0,0},
{4,0,2,0,0,0,0,0,3},
{0,0,0,2,0,1,0,0,0},
{3,0,0,1,8,0,0,9,7},
{0,0,9,0,7,0,6,0,0},
{6,5,0,0,3,2,0,0,1},
{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,8}
} ;
SudokuPuzzle obj = new SudokuPuzzle();
int size = b.length;
System.out.println("The grid is: ");
for(int i =0; i < size; i++)
{
for(int j =0; j < size; j++)
{
System.out.print(b[i][j]+"");
}
System.out.println();
}
System.out.println();
if (obj.solveSudoku(b, size))
{
System.out.println("The solution of the grid is: ");
obj.display(b, size);
}
else
{
System.out.println("There is no solution available.");
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions

Question

3 What are the stages of Kotter and Cohens model of change?

Answered: 1 week ago

Question

4 What is organisation development?

Answered: 1 week ago

Question

5 What activities are employed in OD processes?

Answered: 1 week ago