THINK FIRST·CODE LATER

← All labs

Sudoku referee

Problem

A Sudoku solution is a 9 × 9 grid in which every row, every column and every 3 × 3 box contains the digits 1 to 9 exactly once. SudokuCheck reads the 81 numbers of a filled grid, row by row, and reports every wrong row, column and box (numbered from 1; boxes are numbered left to right, top to bottom), then the verdict.

Write rowIsValid, columnIsValid, boxIsValid(grid, top, left) and a helper mark(boolean[] seen, int digit) that records a digit and returns false if it is not 1–9 or was already seen. For a correct grid the program prints Valid solution. For the input

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 1 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8

the program prints

Row 5 is wrong
Column 5 is wrong
Box 5 is wrong
Invalid solution: 3 problem(s)

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l9-9-data-1.txt

1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 1 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8
What your program must print for l9-9-data-1.txt
Row 5 is wrong
Column 5 is wrong
Box 5 is wrong
Invalid solution: 3 problem(s)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l9-9-data-1.txt

Windows PowerShell does not understand <:

Get-Content l9-9-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.