主题:用pascal解数独
我用pascal编了一个解数独的程序,结果老是没能成功,哪位大师可以帮在下一把啊,帮我找出错误之处!!!谢谢
program exe_1;
type
x=array[1..9,1..9]of integer;
var
a:x;
b:array[1..9,1..9] of boolean;
i,j,y:integer;
t:text;
function yes(i,j:integer; a:x):boolean;
var
k:integer;
begin
yes:=true;
for k:=1 to 9 do
if (a[i,j]=a[i,k]) and (k<>j) then
begin
yes:=false;
exit;
end;
for k:=1 to 9 do
if (a[i,j]=a[k,j]) and (k<>i) then
begin
yes:=false;
exit;
end;
if (i mod 3=1 ) and (j mod 3=1) then
if (a[i,j]=a[i+1,j+1]) or (a[i,j]=a[i+1,j+2]) or
(a[i,j]=a[i+2,j+1]) or (a[i,j]=a[i+2,j+2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=1 ) and (j mod 3=2) then
if (a[i,j]=a[i+1,j-1]) or (a[i,j]=a[i+1,j+1]) or
(a[i,j]=a[i+2,j-1]) or (a[i,j]=a[i+2,j+1]) then
begin
yes:=false;
exit;
end else
if (i mod 3=1 ) and (j mod 3=0) then
if (a[i,j]=a[i+1,j-1]) or (a[i,j]=a[i+1,j-2]) or
(a[i,j]=a[i+2,j-1]) or (a[i,j]=a[i+2,j-2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=2 ) and (j mod 3=1) then
if (a[i,j]=a[i-1,j+1]) or (a[i,j]=a[i-1,j+2]) or
(a[i,j]=a[i+1,j+1]) or (a[i,j]=a[i+1,j+2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=2 ) and (j mod 3=2) then
if (a[i,j]=a[i-1,j-1]) or (a[i,j]=a[i-1,j+1]) or
(a[i,j]=a[i+1,j-1]) or (a[i,j]=a[i+1,j+1]) then
begin
yes:=false;
exit;
end else
if (i mod 3=2 ) and (j mod 3=0) then
if (a[i,j]=a[i-1,j-1]) or (a[i,j]=a[i-1,j-2]) or
(a[i,j]=a[i+1,j-1]) or (a[i,j]=a[i+1,j-2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=0) and (j mod 3=1) then
if (a[i,j]=a[i-1,j+1]) or (a[i,j]=a[i-1,j+2]) or
(a[i,j]=a[i-2,j+1]) or (a[i,j]=a[i-2,j+2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=0 ) and (j mod 3=2) then
if (a[i,j]=a[i-1,j-1]) or (a[i,j]=a[i-1,j+1]) or
(a[i,j]=a[i-2,j-1]) or (a[i,j]=a[i-2,j+1]) then
begin
yes:=false;
exit;
end else
if (i mod 3=0 ) and (j mod 3=0) then
if (a[i,j]=a[i-1,j-1]) or (a[i,j]=a[i-1,j-2]) or
(a[i,j]=a[i-2,j-1]) or (a[i,j]=a[i-2,j-2]) then
begin
yes:=false;
exit;
end;
end;
procedure try(i,j:integer; a:x);
var
k,g:integer;
begin
if j>9 then
begin
i:=i+1;
j:=1;
end;
if (i>9) then
begin
for k:=1 to 9 do
begin
for g:=1 to 9 do write(a[k,g]);
writeln;
end;
inc(y);
writeln;
end
else
begin
if b[i,j] then
begin
for k:=1 to 9 do
begin
a[i,j]:=k;
if yes(i,j,a) then try(i,j+1,a);
end
end
else try(i,j+1,a);
end;
end;
begin
assign(t,'ta.txt');
reset(t);
for i:=1 to 9 do
begin
for j:=1 to 9 do
begin
read(t,a[i,j]);
if (a[i,j]=0) then b[i,j]:=true;
end;
readln(t);
end;
close(t);
y:=0;
try(1,1,a);
writeln(y);
end.
program exe_1;
type
x=array[1..9,1..9]of integer;
var
a:x;
b:array[1..9,1..9] of boolean;
i,j,y:integer;
t:text;
function yes(i,j:integer; a:x):boolean;
var
k:integer;
begin
yes:=true;
for k:=1 to 9 do
if (a[i,j]=a[i,k]) and (k<>j) then
begin
yes:=false;
exit;
end;
for k:=1 to 9 do
if (a[i,j]=a[k,j]) and (k<>i) then
begin
yes:=false;
exit;
end;
if (i mod 3=1 ) and (j mod 3=1) then
if (a[i,j]=a[i+1,j+1]) or (a[i,j]=a[i+1,j+2]) or
(a[i,j]=a[i+2,j+1]) or (a[i,j]=a[i+2,j+2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=1 ) and (j mod 3=2) then
if (a[i,j]=a[i+1,j-1]) or (a[i,j]=a[i+1,j+1]) or
(a[i,j]=a[i+2,j-1]) or (a[i,j]=a[i+2,j+1]) then
begin
yes:=false;
exit;
end else
if (i mod 3=1 ) and (j mod 3=0) then
if (a[i,j]=a[i+1,j-1]) or (a[i,j]=a[i+1,j-2]) or
(a[i,j]=a[i+2,j-1]) or (a[i,j]=a[i+2,j-2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=2 ) and (j mod 3=1) then
if (a[i,j]=a[i-1,j+1]) or (a[i,j]=a[i-1,j+2]) or
(a[i,j]=a[i+1,j+1]) or (a[i,j]=a[i+1,j+2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=2 ) and (j mod 3=2) then
if (a[i,j]=a[i-1,j-1]) or (a[i,j]=a[i-1,j+1]) or
(a[i,j]=a[i+1,j-1]) or (a[i,j]=a[i+1,j+1]) then
begin
yes:=false;
exit;
end else
if (i mod 3=2 ) and (j mod 3=0) then
if (a[i,j]=a[i-1,j-1]) or (a[i,j]=a[i-1,j-2]) or
(a[i,j]=a[i+1,j-1]) or (a[i,j]=a[i+1,j-2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=0) and (j mod 3=1) then
if (a[i,j]=a[i-1,j+1]) or (a[i,j]=a[i-1,j+2]) or
(a[i,j]=a[i-2,j+1]) or (a[i,j]=a[i-2,j+2]) then
begin
yes:=false;
exit;
end else
if (i mod 3=0 ) and (j mod 3=2) then
if (a[i,j]=a[i-1,j-1]) or (a[i,j]=a[i-1,j+1]) or
(a[i,j]=a[i-2,j-1]) or (a[i,j]=a[i-2,j+1]) then
begin
yes:=false;
exit;
end else
if (i mod 3=0 ) and (j mod 3=0) then
if (a[i,j]=a[i-1,j-1]) or (a[i,j]=a[i-1,j-2]) or
(a[i,j]=a[i-2,j-1]) or (a[i,j]=a[i-2,j-2]) then
begin
yes:=false;
exit;
end;
end;
procedure try(i,j:integer; a:x);
var
k,g:integer;
begin
if j>9 then
begin
i:=i+1;
j:=1;
end;
if (i>9) then
begin
for k:=1 to 9 do
begin
for g:=1 to 9 do write(a[k,g]);
writeln;
end;
inc(y);
writeln;
end
else
begin
if b[i,j] then
begin
for k:=1 to 9 do
begin
a[i,j]:=k;
if yes(i,j,a) then try(i,j+1,a);
end
end
else try(i,j+1,a);
end;
end;
begin
assign(t,'ta.txt');
reset(t);
for i:=1 to 9 do
begin
for j:=1 to 9 do
begin
read(t,a[i,j]);
if (a[i,j]=0) then b[i,j]:=true;
end;
readln(t);
end;
close(t);
y:=0;
try(1,1,a);
writeln(y);
end.