Listing 8.4 checks whether a solution is valid by checking whether every number is valid in the
Question:
Listing 8.4 checks whether a solution is valid by checking whether every number is valid in the board. Rewrite the program by checking whether every row, every column, and every small box has the numbers 1 to 9.
Listing
Transcribed Image Text:
1 import java.util.Scanner; 2 3 public class CheckSudokuSolution { public static void main(String[] args) { // Read a Sudoku solution int [] [] grid = readASolution(); 4 6 System.out.printIn(isValid(grid) ? "Valid solution" : "Invalid solution"); 8. 6. 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /** Read a Sudoku solution from the console */ public static int[][] readASolution() { // Create a Scanner Scanner input = new Scanner(System.in); System.out.println("Enter a Sudoku puzzle solution:"); int [][] grid = new int[9] [9]; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) grid[i][j] = input.nextInt(); return grid; 26 /** Check whether a solution is valid */ 27 public static boolean isValid(int[][] grid) { HNm 4567 ~~~2N~~ for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) if (grid[i][j] < 1 || grid[i][j] > 9 Il !isValid(i, j, grid)) return false; return true; // The solution is valid 28 29 30 31 32 33 34 35 36 /** Check whether grid[i][j] is valid in the grid */ public static boolean isValid(int i, int j, int[][] grid) { // Check whether grid[i][j] is unique in i's row for (int column = 0; column < 9; column++) if (column != j && grid[i][co]umn] == grid[i][j]) return false; 37 38 39 40 41 42 43 // Check whether grid[i][j] is unique in j's column for (int row = 0; row < 9; row++) if (row != i && grid[row][j] == grid[i][j]) return false; 44 45 46 47 48 49 50 51 52 53 54 // Check whether grid[i][j] is unique in the 3-by-3 box for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++) for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++) if (row != i && col != j && grid[row][col] == grid[i][j]) return false; return true; // The current value at grid[i][j] is valid 55 } 56 } Enter a Sudoku puzzle solution: 9 6 3 17 4 2 5 8 -Enter 17 8 3 2 5 6 4 9 JEnter 25 4 6 8 97 31 -Enter 8 2 1 4 3 759 6 JEnter 4 9 6 8 52 317 JEnter 7 3596 1 8 2 4 -Enter 5 89 7 13 4 6 2 JEnter 3 17 2 4 69 8 5 -Enter 6 4 2 5 9 8 17 3 Valid solution LEnter
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 40% (10 reviews)
Program Plan Create readSolution method so that reads the solution from the user input and returns t...View the full answer
Answered By
Muhammad Umair
I have done job as Embedded System Engineer for just four months but after it i have decided to open my own lab and to work on projects that i can launch my own product in market. I work on different softwares like Proteus, Mikroc to program Embedded Systems. My basic work is on Embedded Systems. I have skills in Autocad, Proteus, C++, C programming and i love to share these skills to other to enhance my knowledge too.
3.50+
1+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Using your solution to problem 8, do the following: a. Add the view layer classes and the data access classes to your diagram. b. Develop a package diagram showing a three layer solution with view...
-
Listing 5.15 determines whether a number n is prime by checking whether 2, 3, 4, 5, 6, ..., n/2 is a divisor. If a divisor is found, n is not prime. A more efficient approach is to check whether any...
-
Lexsteel, a manufacturer of steel furniture, has facilities throughout the United States. Problems with the accounts payable system have prompted Lexsteel's external auditor to recommend a detailed...
-
Briefly explain the meaning of the following concepts and terms: greenhouse gases (GHGs), the IPCC, the little ice age, the Keeling graph, telekinetic property of the atmosphere, global weirding,...
-
If x y P(x, y) is true, does it necessarily follow that x y P(x, y) is true?
-
(a) You are given the task of constructing a double-slit experiment for 5 eV electrons. If you want the first minimum of the diffraction pattern to occur at 5, what must be the separation of the...
-
The following business transactions represent selected cash receipts (cash inflows) and cash payments (cash outflows) of Ridge Development Corporation. a. dividend payment b. payment of insurance...
-
Your company has a customer who is shutting down a production line, and it is your responsibility to dispose of the extrusion machine. The company could keep it in inventory for possible future...
-
3. Using the straight line method - construct the annual depreciation table for Company X given the information below from 2018-2021. Cost ..............................................................
-
Reba Dixon is a fifth-grade schoolteacher who earned a salary of $38,000 in 2015. She is 45 years old and has been divorced for four years. She received $1,200 of alimony payments each month from her...
-
Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program...
-
An n ? n matrix is called a positive Markov matrix if each element is positive and the sum of the elements in each column is 1. Write the following method to check whether a matrix is a Markov...
-
Many farmers growing corn using conventional methods still choose to apply B. thuringiensis sprays. What are the advantages of this strategy for controlling ECBs?
-
Assume we are January 1,2024. Samuel turned 20 years old on December 31,2023, while his twin sister, Samantha turned 20 years old on January 1,2024. For their special 21St birthday, their grandmother...
-
The wing with aspect ratio A = 5 has a drag coefficient Cp = 0.01 when the lift coefficient is C = 0.8. Evaluate the drag coefficient for the wing with A = 10 at the same value of lift coefficient....
-
Suppose that the following simultaneous move game is played twice, with the final payoffs being the sum of the payoffs obtained in the two stages of the game: LCR T 10,105,15 0,0 M 15,5 7,7 0,0 B 0,0...
-
Calculate the annual nominal return (2010-2020) for the stock (in decimals). 2. Calculate the annual nominal return (2010-2020) for the market (in decimals). 3. Present the annual nominal return...
-
At what separation will two charges, each of magnitude 6 . 4 0 \ mu C , exert a force of 3 . 6 0 N on each other? Give your answer in meters. ( k = 1 / 4 \ pi \ epsi 0 = 9 . 0 0 \ times 1 0 9 N m 2 /...
-
Suppose that you have $900 and what to invest the money for one year. There are three existing options. (a) The city of Rochester is selling bonds at $90 per unit. The bonds pay $100 at the end of...
-
The Home Depot is the leading retailer in the home improvement industry and one of the 10largest retailers in the United States. The company included the following on its January 29, 2012, balance...
-
You modified the RentalDemo program for Sammys Seashore Supplies to accept and display data for an array of three Rental objects. Now, modify the program to use an array of eight Rental objects....
-
A reliable way to discover which of two classes is the base class and which is the subclass is to ____________. a. Look at the class size b. Try saying the two class names together c. Use...
-
Employing inheritance reduces errors because ____________. a. Subclasses have access to fewer data fields b. Subclasses have access to fewer methods c. Methods that have been created in the...
-
Question 60 (1 point) Do the pairings in the following chart to indicate the per acre value for the subject? Sale No. Subject A B C D Price/Ac $1,800 $1,600 $2,100 $1,700 Location Paved Dirt Dirt...
-
n the data set below, what is the mean (mc027-1.jpg)? X f 5 4 4 2 3 1 2 1 1 2 Question 29Answer a. 4 b. 3.5 c. 3 d. 2.5
-
ped Saving for College Monthly (End and Beg) The parents of Mr. and Mrs. Smith have decided to give their newborn grandchild $177 at the end of each month for the next 18 years to help pay for...
Study smarter with the SolutionInn App