主题:[原创]数独
其实我有一个同学比我还早写数独,但是他只给了程序,没代码。因此我自己在2007-02-16,1100-1200这一个小时里研究出了程序的写法,有改进方法的请去http://mytbk.9yz.com.cn/bbs或发邮件至tbk21@163.com或回贴。
var
a:array [1..9,1..9] of byte;
b:array [1..81] of integer;
i,j:integer;
n:integer;
f_in,f_out:text;
fname_in,fname_out:string;
function ok(x:integer):boolean;
var i,j,xa,ya:integer;
begin
ok:=true;
xa:=x div 10;
ya:=x mod 10;
for i:=1 to 9 do
begin
if (a[i][ya]=a[xa][ya]) and (i<>xa) then ok:=false;
if (a[xa][i]=a[xa][ya]) and (i<>ya) then ok:=false;
end;
for i:=((xa+2) div 3-1)*3+1 to ((xa+2) div 3-1)*3+3 do
for j:=((ya+2) div 3-1)*3+1 to ((ya+2) div 3-1)*3+3 do if (a[i][j]=a[xa][ya]) and ((i<>xa) or (j<>ya)) then ok:=false;
end;
begin {program}
writeln ('/--------------------------------------------------------\');
writeln ('|Sudoku solver v1.0 by Iru Dog |');
writeln ('|Copyright 2003-2007 Tbk Corporation,All rights reserved.|');
writeln ('\--------------------------------------------------------/');
writeln;
write ('file input:');
readln (fname_in);
assign (f_in,fname_in);
reset (f_in);
for i:=1 to 9 do
for j:=1 to 9 do read (f_in,a[i][j]);
close (f_in);
n:=0;
for i:=1 to 9 do
for j:=1 to 9 do
begin
if a[i][j]=0 then
begin
n:=n+1;
b[n]:=i*10+j;
end;
end;
i:=1;
while i<=n do
begin
a[b[i] div 10][b[i] mod 10]:=a[b[i] div 10][b[i] mod 10]+1;
if a[b[i] div 10][b[i] mod 10]=10 then
begin
a[b[i] div 10][b[i] mod 10]:=0;
i:=i-1;
end
else if ok(b[i]) then i:=i+1;
end;
write ('file output:');
readln (fname_out);
assign (f_out,fname_out);
rewrite (f_out);
for i:=1 to 9 do
begin
for j:=1 to 9 do write (f_out,a[i][j]:2);
writeln (f_out);
end;
close (f_out);
writeln ('Look at ',fname_out,',please!');
writeln ('Thank you for using Sudoku Solver!');
end.
{input_example.txt
0 0 9 0 3 0 1 0 0
7 5 0 0 0 0 0 4 9
0 1 0 0 0 0 0 7 0
8 0 0 2 6 1 0 0 7
0 0 0 0 0 0 0 0 0
4 0 0 5 9 7 0 0 3
0 8 0 0 0 0 0 5 0
2 6 0 0 0 0 0 1 8
0 0 7 0 8 0 6 0 0}
{本程序在turbo pascal 7及fpc.exe下编译通过}
var
a:array [1..9,1..9] of byte;
b:array [1..81] of integer;
i,j:integer;
n:integer;
f_in,f_out:text;
fname_in,fname_out:string;
function ok(x:integer):boolean;
var i,j,xa,ya:integer;
begin
ok:=true;
xa:=x div 10;
ya:=x mod 10;
for i:=1 to 9 do
begin
if (a[i][ya]=a[xa][ya]) and (i<>xa) then ok:=false;
if (a[xa][i]=a[xa][ya]) and (i<>ya) then ok:=false;
end;
for i:=((xa+2) div 3-1)*3+1 to ((xa+2) div 3-1)*3+3 do
for j:=((ya+2) div 3-1)*3+1 to ((ya+2) div 3-1)*3+3 do if (a[i][j]=a[xa][ya]) and ((i<>xa) or (j<>ya)) then ok:=false;
end;
begin {program}
writeln ('/--------------------------------------------------------\');
writeln ('|Sudoku solver v1.0 by Iru Dog |');
writeln ('|Copyright 2003-2007 Tbk Corporation,All rights reserved.|');
writeln ('\--------------------------------------------------------/');
writeln;
write ('file input:');
readln (fname_in);
assign (f_in,fname_in);
reset (f_in);
for i:=1 to 9 do
for j:=1 to 9 do read (f_in,a[i][j]);
close (f_in);
n:=0;
for i:=1 to 9 do
for j:=1 to 9 do
begin
if a[i][j]=0 then
begin
n:=n+1;
b[n]:=i*10+j;
end;
end;
i:=1;
while i<=n do
begin
a[b[i] div 10][b[i] mod 10]:=a[b[i] div 10][b[i] mod 10]+1;
if a[b[i] div 10][b[i] mod 10]=10 then
begin
a[b[i] div 10][b[i] mod 10]:=0;
i:=i-1;
end
else if ok(b[i]) then i:=i+1;
end;
write ('file output:');
readln (fname_out);
assign (f_out,fname_out);
rewrite (f_out);
for i:=1 to 9 do
begin
for j:=1 to 9 do write (f_out,a[i][j]:2);
writeln (f_out);
end;
close (f_out);
writeln ('Look at ',fname_out,',please!');
writeln ('Thank you for using Sudoku Solver!');
end.
{input_example.txt
0 0 9 0 3 0 1 0 0
7 5 0 0 0 0 0 4 9
0 1 0 0 0 0 0 7 0
8 0 0 2 6 1 0 0 7
0 0 0 0 0 0 0 0 0
4 0 0 5 9 7 0 0 3
0 8 0 0 0 0 0 5 0
2 6 0 0 0 0 0 1 8
0 0 7 0 8 0 6 0 0}
{本程序在turbo pascal 7及fpc.exe下编译通过}