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?
-
Describe how pressures to have desirable short-run outcomes can lead to ethical dilemmas. (p. 21)
-
Consider the investment decision in Exercise 14.48. What qualitative factors might be critical to evaluating this decision? If the lowest-cost alternative is $70,000 and if the discount rate is 8...
-
Included in the December 31, 2009 Jacobi Company balance sheet was the following stockholders equity section: The company engaged in the following stock transactions during 2010: Jan. 4 Paid the...
-
the numbers i have plugged in under the amount collum are incorrect minus the common stock I think 2. Prepare a horizontal analysis of Sports Unlimited's 2021 balance sheet using 2020 as the base...
-
A small British computer-game firm, Eidos Interactive PLC, stunned the U.S.- and Japan-dominated market for computer games when it introduced Lara Croft, an Indiana Jones-like adventuress. The...
-
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...
-
St. Paul Co. does business in the United States and New Zealand. In attempting to assess its economic exposure, it compiled the following information. a. St. Pauls U.S. sales are somewhat affected by...
-
How do you do the step method in cost accounting? I need help understanding the difference between the step method and reciprocal method.
-
1- How do long-term care changing the health care system in the 21st century? What ethical issues do you see playing a role in this situation? 2- Why do long-term health care and palliative care...
-
How do the processes and layouts of Harley Davidson enable it to deliver goods and services to its customer? (How do they do what they do with the resources they have?)
-
The State Public Works Division consists of the Administrator, the State Public Works Board, the Public Works Section, and the Buildings and Grounds Section. The State Public Works Board consists of...
-
Everyone knows that health care costs are high. It is also known that people tend to spend less on health care if they spend their own money, which motivated the creation of flexible spending...
-
Identify a situation in your life where someone used rule-based ethical reasoning.
-
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...
-
REQUIRMENT 1: 1. record 380,000 shares of common stock were issued at $15.40 per share 2. record 90,000 shares of treasuery (common) stock were sold for $19.4 per share 3. Record net income for the...
-
Lucy takes an early distribution of $12,000 from her IRA and uses the funds to help purchase her first home. How much, if any, of the $12,000 is subject to the early withdrawal penalty?.
-
1. a. What are the three costs facing a household saver who chooses to invest directly in corporate securities? Explain. b. When financial institutions (FIs) act as intermediaries between the...
Study smarter with the SolutionInn App