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: 50% (6 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 current of 11.3 A is applied to 1.25 L of a solution of 0.552 M HBr converting some of the H + to H 2 (g), which bubbles out of solution. What is the pH of the solution after 73 minutes?
-
What are some indicators that a digital device has been infected?
-
The December 31, 2013, adjusted trial balance of Success Systems (reflecting its transactions from October through December of 2013) follows. Required 1. Record and post the necessary closing entries...
-
Executives at Southwestern Construction have noticed that the company's construction team in the Phoenix office is more efficient with its resources than the other teams in the Las Vegas, Salt Lake,...
-
The time it takes an international telephone operator to place an overseas phone call is normally distributed with mean 45 seconds and standard deviation 10 seconds. a. What is the probability that...
-
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...
-
Solve the given equations. x - 4 x - 1 2 X
-
The opening case explores the globalization of the bike industry. In the 1970s, the United States produced some 15 million bikes every year. Bike 2018 however, about 95 percent of the bikes sold in...
-
Research the country of Mexico and answer the questions below in depth. . Discuss the standard of living (What do the people own? How does the standard of living compare to the U.S.? Ideally, this...
-
Tailwinds and Headwinds in the Global Economy International developments like the easing of the pandemic and war in Ukraine, China's reopening, US still growing, and Europe picking up face suggest a...
-
Adam Smith, "Of the Wages of Labour" Book I, Chapter VIII Wealth of Nations (1776) According to Smith: Were there laws to restrict the reduction of wages? To restrict an increase in wages? Under what...
-
how economic understanding improves stewardship? Economics is often over looked by Christians and is largely missing from most world view discussions. The reasons for this are probably varied but...
-
Describe how a product or service will fit into and complement your marketing mix?
-
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...
-
Prepare a written network design plan to meet the above mentioned user requirements including a blueprint drawn using a modeling tool. (Ex: Microsoft Visio, Edraw Max). Support your answer by...
-
There is already one LAN at the KL site => 172.16.32.0/28. There are plans to have two more LANs at the KL site, LANs 2 and 3. Both of LANs 2 and 3 will be subnets of 172.16.32.0/24. LAN 2 requires...
-
Wikipedia is not actually produced in a competitive market, but instead is largely funded by voluntary individual donations from its users. Those users usually get a lot of value out of the existence...
Study smarter with the SolutionInn App