Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Here I have a code that displays and gives solution so how can I amke it so I am righting my answer but if its
Here I have a code that displays and gives solution so how can I amke 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?
import java.util.Scanner;
public class Sudoku
public boolean isSafeint b int r int c int num
Check if 'num' is not present in the current row and column
for int i ; i ; i
if bri num bic num
return false;
Check if 'num' is not present in the x grid
int startRow r r ;
int startCol c c ;
for int i ; i ; i
for int j ; j ; j
if bi startRowj startCol num
return false;
return true;
public boolean solveSudokuint b
int n blength;
Find an empty location
int empty findEmptyLocationb;
int row empty;
int col empty;
If there is no empty location, the puzzle is solved
if row && col
return true;
Try filling the empty location with a number
for int num ; num ; num
if isSafeb row, col, num
browcol num;
Recursively try to solve the rest of the puzzle
if solveSudokub
return true;
If placing 'num' at the current location doesn't lead to a solution, backtrack
browcol;
If no number can be placed at the current location, backtrack
return false;
private int findEmptyLocationint b
int location new int;
for int i ; i ; i
for int j ; j ; j
if bij
location i;
location j;
return location;
return location;
public void displayint b
for int i ; i ; i
for int j ; j ; j
System.out.printbij;
System.out.println;
public static void mainString args
Scanner scanner new ScannerSystemin;
Take user input for the initial Sudoku grid
System.out.printlnEnter the Sudoku grid for empty cells:;
int b new int;
for int i ; i ; i
for int j ; j ; j
bij scanner.nextInt;
Sudoku obj new Sudoku;
System.out.printlnThe initial grid is: ;
obj.displayb;
if objsolveSudokub
System.out.printlnThe solution of the grid is: ;
obj.displayb;
else
System.out.printlnThere is no solution available.";
scanner.close;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started