program SudokuGridValidation;
// Sudoku solution must satisfy all of the following rules:
// Each of the digits 1-9 must occur once in each row.
// Each of the digits 1-9 must occur once in each column.
// Each of the digits 1-9 must occur once in each 3x3 grid.
const
SIZE = 3;
type
TMatrix = array[1..SIZE, 1..SIZE] of Integer;
function IsValidSudoku3x3Grid(matrix: TMatrix): Boolean;
var
seen: array[1..9] of Boolean;
i, j, num: Integer;
begin
// Initialize seen array
for num := 1 to 9 do
seen[num] := False;
// Validate the grid
for i := 1 to SIZE do
for j := 1 to SIZE do
begin
num := matrix[i, j];
if (num < 1) or (num > 9) or seen[num] then
Exit(False);
seen[num] := True;
end;
IsValidSudokuGrid := True;
end;
procedure PrintResult(valid: Boolean);
begin
if valid then
Writeln('The grid is a valid Sudoku grid!')
else
Writeln('The grid is NOT a valid Sudoku grid!');
end;
var
matrix: TMatrix = (
(5, 3, 4),
(6, 7, 2),
(1, 9, 8)
);
begin
PrintResult(IsValidSudoku3x3Grid(matrix));
end.
(*
run:
The grid is a valid Sudoku grid!
*)