unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Memo2: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k:integer;
adj,adv:integer;
ss:string;
blline,blfind:boolean;
matrix: array [1..9,1..9] of integer;
mcopy:array [1..9] of integer;
//------------------------------------
//列检测
function checkrownum(row:integer):boolean;
var
i:integer;
count:array [1..9] of integer;
begin
for i:=1 to 9do
count:=0;
for i:=1 to 9do
begin
if (matrix[i,row]>0) and (matrix[i,row]<10) then
inc(count[matrix[i,row]]);
end;
result:=true;
for i:=1 to 9do
begin
if count>1 then
begin
result:=false;
break;
end;
end;
end;
//------------------------------------
//行检测
function checkcownum(cow:integer):boolean;
var
i:integer;
count:array [1..9] of integer;
begin
for i:=1 to 9do
count:=0;
for i:=1 to 9do
begin
if (matrix[cow,i]>0) and (matrix[cow,i]<10) then
inc(count[matrix[cow,i]]);
end;
result:=true;
for i:=1 to 9do
begin
if count>1 then
begin
result:=false;
break;
end;
end;
end;
//------------------------------------
begin
adv:=0;
adj:=0;
while truedo
begin
//初始化
for i:=1 to 9do
for j:=1 to 9do
begin
matrix[i,j]:=0;
end;
//初始值
// 1 2 3 4 5 6 7 8 9
//1 6 2 7
//2 5
//3 4
//4 1
//5 4
//6 3
//7 1
//8 9
//9 8
//赋初值
matrix[1,1]:=6;
matrix[1,5]:=2;
matrix[1,9]:=7;
matrix[2,3]:=5;
matrix[3,2]:=4;
matrix[4,7]:=1;
matrix[5,1]:=4;
matrix[6,5]:=3;
matrix[7,1]:=1;
matrix[8,9]:=9;
matrix[9,1]:=8;
//检测初值的正确性
for i:=1 to 9do
begin
if not checkrownum(i) then
showmessage('初始化error');
if not checkcownum(i) then
showmessage('初始化error');
end;
//显示初值
memo1.Lines.Clear;
for i:=1 to 9do
begin
ss:='';
for j:=1 to 9do
begin
ss:=ss+' '+inttostr(matrix[i,j]);
end;
memo1.Lines.Add(ss);
end;
//运算
//checkrownum(j) and checkcownum(i)
//快速填充
for i:=1 to 9do
begin
//复制行副本
for j:=1 to 9do
mcopy[j]:=matrix[i,j];
adj:=adv;
while truedo
begin
//保证此行正确
blline:=true;
for j:=1 to 9do
if matrix[i,j]=0 then
begin
blfind:=false;
for k:=0 to 8do
begin
matrix[i,j]:=((k+adj) mod 9)+1;
if checkrownum(j) and checkcownum(i) then
begin
blfind:=true;
break;
end;
end;
if not blfind then
begin
blline:=false;
break;
end;
end;
if blline then
break
else
begin
inc(adj);
for j:=1 to 9do
matrix[i,j]:=mcopy[j];
if adj>18 then
break;
end;
end;
//end while
end;
//验证结果
adj:=0;
for i:=1 to 9do
for j:=1 to 9do
begin
if matrix[i,j]=0 then
begin
inc(adj);
break;
end;
end;
//结果判断
if adj>0 then
begin
inc(adv);
if adv>9 then
break;
end
else
break;
end;
//显示结果
memo2.Lines.Clear;
//显示adv
memo2.Lines.Add('========adv='+inttostr(adv)+'===========');
if adj=0 then
begin
for i:=1 to 9do
begin
ss:='';
for j:=1 to 9do
begin
ss:=ss+' '+inttostr(matrix[i,j]);
end;
memo2.Lines.Add(ss);
end;
end
else
begin
memo2.Lines.Add('无解');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i,j,k:integer;
adj,adv:integer;
ss:string;
blline,blfind:boolean;
matrix: array [1..9,1..9] of integer;
mcopy:array [1..9] of integer;
//------------------------------------
//列检测
function checkrownum(row:integer):boolean;
var
i:integer;
count:array [1..9] of integer;
begin
for i:=1 to 9do
count:=0;
for i:=1 to 9do
begin
if (matrix[i,row]>0) and (matrix[i,row]<10) then
inc(count[matrix[i,row]]);
end;
result:=true;
for i:=1 to 9do
begin
if count>1 then
begin
result:=false;
break;
end;
end;
end;
//------------------------------------
//行检测
function checkcownum(cow:integer):boolean;
var
i:integer;
count:array [1..9] of integer;
begin
for i:=1 to 9do
count:=0;
for i:=1 to 9do
begin
if (matrix[cow,i]>0) and (matrix[cow,i]<10) then
inc(count[matrix[cow,i]]);
end;
result:=true;
for i:=1 to 9do
begin
if count>1 then
begin
result:=false;
break;
end;
end;
end;
//------------------------------------
begin
adv:=0;
adj:=0;
while truedo
begin
//初始化
for i:=1 to 9do
for j:=1 to 9do
begin
matrix[i,j]:=0;
end;
//初始值
// 1 2 3 4 5 6 7 8 9
//1 6 2 7
//2 5
//3 4
//4 1
//5 4
//6 3
//7 1
//8 9
//9 8
//赋初值
matrix[1,1]:=6;
matrix[1,5]:=2;
matrix[1,9]:=7;
matrix[2,3]:=5;
matrix[3,2]:=4;
matrix[4,7]:=1;
matrix[5,1]:=4;
matrix[6,5]:=3;
matrix[7,1]:=1;
matrix[8,9]:=9;
matrix[9,1]:=8;
//检测初值的正确性
for i:=1 to 9do
begin
if not checkrownum(i) then
showmessage('初始化error');
if not checkcownum(i) then
showmessage('初始化error');
end;
//显示初值
memo1.Lines.Clear;
for i:=1 to 9do
begin
ss:='';
for j:=1 to 9do
begin
ss:=ss+' '+inttostr(matrix[i,j]);
end;
memo1.Lines.Add(ss);
end;
//运算
//checkrownum(j) and checkcownum(i)
//快速填充
for i:=1 to 9do
begin
//复制行副本
for j:=1 to 9do
mcopy[j]:=matrix[i,j];
adj:=adv;
while truedo
begin
//保证此行正确
blline:=true;
for j:=1 to 9do
if matrix[i,j]=0 then
begin
blfind:=false;
for k:=8do
wnto 0do
begin
matrix[i,j]:=((k+adj) mod 9)+1;
if checkrownum(j) and checkcownum(i) then
begin
blfind:=true;
break;
end;
end;
if not blfind then
begin
blline:=false;
break;
end;
end;
if blline then
break
else
begin
inc(adj);
for j:=1 to 9do
matrix[i,j]:=mcopy[j];
if adj>18 then
break;
end;
end;
//end while
end;
//验证结果
adj:=0;
for i:=1 to 9do
for j:=1 to 9do
begin
if matrix[i,j]=0 then
begin
inc(adj);
break;
end;
end;
//结果判断
if adj>0 then
begin
inc(adv);
if adv>9 then
break;
end
else
break;
end;
//显示结果
memo2.Lines.Clear;
//显示adv
memo2.Lines.Add('========adv='+inttostr(adv)+'===========');
if adj=0 then
begin
for i:=1 to 9do
begin
ss:='';
for j:=1 to 9do
begin
ss:=ss+' '+inttostr(matrix[i,j]);
end;
memo2.Lines.Add(ss);
end;
end
else
begin
memo2.Lines.Add('无解');
end;
end;
end.