Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

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 isSafe (int[][] b, int r, int c, int num) { // Check if 'num' is not present in the current row and column for (int i = 0; i < 9; i++) { if (b[r][i] = num || b[i][ c] == num) { return false; } } // Check if 'num' is not present in the 3x3 grid int startRow = r (r% 3); int startCol = c (c% 3); for (int i =0;i<3;i++)\{ for (int j = 0; j < 3; j++) { if (b[i + startRow][j + startCol] == num) { return false; } } } return true; } public boolean solveSudoku (int[][] b) { int n = b.length; // Find an empty location int empty = findEmpty Location(b); int row = empty[0]; int col = empty [1]; // If there is no empty location, the puzzle is solved if (row ==-1~88~col=-1 { return true; } // Try filling the empty location with a number for (int num = 1; num <= 9; num++) { if ( isSafe(b, row, col, num)) { b[row][col] = num; // Recursively try to solve the rest of the puzzle if (solveSudoku(b)) { return true; } // If placing 'num' at the current location doesn't lead to a solution, backtrack b[row][col]=0;\}// If no number can be placed at the current location, backtrack return false; } private int[] findEmptyLocation(int[][] b) { int[] location = new int[]{1, 1); for ( int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (b[i][j] == 0) { location [0]=i;l location[1] = j; return location; }}} return location; } public void display (int[][] b) { for (int i = 0; i < 9; i++) { for (int j = 0 ;j< 9; j++) { System.out.print(b[i][j]+""); } System.out.println(); } }

public static void main(String[] args) { Scanner scanner = new

Scanner(System.in); // Take user input for the initial Sudoku grid

System.out.println("Enter the Sudoku grid (0 for empty cells):"); int[][]

b = new int[9][9]; for (int~i=0;i<9;i++). for (int~j=0;j<9;j

++) { b[i][j] = scanner.nextInt(); } } Sudoku obj = new Sudoku();

System.out.println("The initial grid is: "); obj.display(b); if (obj.

solveSudoku(b)) { System.out.println("The solution of the grid is: ");

obj.display(b); } else { System.out.println("There is no solution

available."); } scanner.close(); } }

 

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education