{$MODE OBJFPC} { -*- delphi -*- } {$INCLUDE settings.inc} program sudoku; uses {$IFDEF DEBUG} debug, {$ENDIF} sysutils; const SideSize = 9; BoxSize = 3; type TDigit = 1..9; TDigits = set of TDigit; TPosition = 0..SideSize-1; TGrid = array[TPosition, TPosition] of TDigits; TSolutionCount = (scInvalid, scUnique, scAmbiguous); function BlankGrid(): TGrid; var X, Y: Integer; begin for X := 0 to SideSize - 1 do begin for Y := 0 to SideSize - 1 do begin Result[X, Y] := [Low(TDigit) .. High(TDigit)]; end; end; end; function PlaceDigit(Grid: TGrid; const Digit: TDigit; const DigitX, DigitY: TPosition): TGrid; var X, Y, BoxX, BoxY: Integer; begin Assert(Digit in Grid[DigitX, DigitY]); Assert(Grid[DigitX, DigitY] - [Digit] <> []); // remove everything else in this cell Grid[DigitX, DigitY] := [Digit]; // remove from row for X := 0 to SideSize - 1 do begin if (X <> DigitX) then begin Exclude(Grid[X, DigitY], Digit); end; end; // remove from column for Y := 0 to SideSize - 1 do begin if (Y <> DigitY) then begin Exclude(Grid[DigitX, Y], Digit); end; end; // remove from box BoxX := (DigitX mod BoxSize) * BoxSize; // $R- BoxY := (DigitY mod BoxSize) * BoxSize; // $R- for X := BoxY to BoxY + BoxSize - 1 do // $R- begin for Y := BoxX to BoxX + BoxSize - 1 do // $R- begin if ((X <> DigitX) and (Y <> DigitY)) then begin Exclude(Grid[X, Y], Digit); end; end; end; Result := Grid; end; function Solve(Grid: TGrid): TSolutionCount; begin end; function PlaceDigit(Grid: TGrid): TGrid; begin end; procedure WriteGrid(Grid: TGrid); var X, Y: Integer; begin for Y := 0 to SideSize - 1 do begin for X := 0 to SideSize - 1 do begin if (Grid[X, Y] Write(Grid[X, Y]:3); end; Writeln(); end; end; begin Writeln(BlankGrid()); end.