000 004 020009 000 000050 200 000060 000 000001 002 007000 008 150014 090 000000 000 030000 800 904
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 03:55:57
000 004 020009 000 000050 200 000060 000 000001 002 007000 008 150014 090 000000 000 030000 800 904
000 004 020
009 000 000
050 200 000
060 000 000
001 002 007
000 008 150
014 090 000
000 000 030
000 800 904
000 004 020009 000 000050 200 000060 000 000001 002 007000 008 150014 090 000000 000 030000 800 904
7 8 6 5 1 4 3 2 9
2 4 9 3 7 6 5 8 1
1 5 3 2 8 9 4 7 6
4 6 5 1 3 7 8 9 2
8 3 1 9 5 2 6 4 7
9 2 7 6 4 8 1 5 3
5 1 4 7 9 3 2 6 8
6 9 8 4 2 1 7 3 5
3 7 2 8 6 5 9 1 4
附解题程序代码
program zk;{20:16 -22:12}
const n=9;
var
i,j,a,b,c,t,p:longint;
map:array [1..9,1..9] of longint;
hang,lie,gong:array [1..9,1..9] of boolean;
h,l,g:array [1..9] of longint;
ok:boolean;
wei:array [1..9,1..9] of longint=((1,1,1,2,2,2,3,3,3),(1,1,1,2,2,2,3,3,3),(1,1,1,2,2,2,3,3,3),(4,4,4,5,5,5,6,6,6),(4,4,4,5,5,5,6,6,6),(4,4,4,5,5,5,6,6,6),(7,7,7,8,8,8,9,9,9),(7,7,7,8,8,8,9,9,9),(7,7,7,8,8,8,9,9,9));
lin,lll:array [1..10] of longint;
procedure print;
var
a,b:longint;
begin
for a:=1 to 9 do
begin
for b:=1 to 9 do
write(map[a,b],' ');
writeln;
end;
end;
procedure sou(k,z:longint);
var
s:longint;
begin
if z=10 then begin inc(k); z:=1; end;
if (k=10) and (z=1) then begin print; ok:=true; exit; end;
if k=10 then exit;
if map[lin[k],lll[z]]=0
then
for s:=1 to 9 do
if (hang[lin[k],s]=false) and (gong[wei[lin[k],lll[z]],s]=false) and (lie[lll[z],s]=false) then
begin
map[lin[k],lll[z]]:=s;
hang[lin[k],s]:=true; lie[lll[z],s]:=true; gong[wei[lin[k],lll[z]],s]:=true;
sou(k,z+1);
if ok then exit;
hang[lin[k],s]:=false; lie[lll[z],s]:=false; gong[wei[lin[k],lll[z]],s]:=false;
map[lin[k],lll[z]]:=0;
end else
else sou(k,z+1);
end;
begin
assign(input,'sudoku.in');
assign(output,'sudoku.out');
reset(input);
rewrite(output);
readln(t);
for p:=1 to t do
begin
for i:=1 to 9 do
begin
lin[i]:=i;
lll[i]:=i;
end;
fillchar(hang,sizeof(hang),false);
fillchar(lie,sizeof(lie),false);
fillchar(gong,sizeof(gong),false);
fillchar(h,sizeof(h),0);
fillchar(l,sizeof(l),0);
fillchar(g,sizeof(g),0);
ok:=false;
for i:=1 to 9 do
begin
for j:=1 to 9 do
begin
read(map[i,j]);
if map[i,j]0 then
begin
hang[i,map[i,j]]:=true; inc(h[i]);
lie[j,map[i,j]]:=true; inc(l[j]);
gong[wei[i,j],map[i,j]]:=true; inc(g[wei[i,j]]);
end;
end;
readln;
end;
for i:=1 to n do
for j:=i to n do
if h[i]