Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 23941 Accepted: 11151 Special Judge
Description
Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.
Input
The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.
Output
For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.
Sample Input
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
深搜+剪枝
var
map:array[0..16,0..16] of char;
t:array[0..16,0..16,'1'..'9'] of boolean;
tot:array['1'..'9'] of longint;
i,j,n,m,l,r,ii,jj,kk,sl,kkk,iii:longint;
tt,key,bii:boolean;
cc:char;
s:string;
function check:boolean;
var
i,j:longint;
begin
for i:=1 to n do
for j:=1 to n do
if map[i,j]='0' then exit(false);
exit(true);
end;
function work(x:longint):longint;
begin
if x<4 then exit(1);
if x<7 then exit(4);
exit(7);
end;
procedure fill(x,y:longint;c:char);
var i,j,l,r:longint;
begin
map[x,y]:=c;
for i:=1 to n do
begin
t[x,i,c]:=false;
t[i,y,c]:=false;
end;
l:=work(x);
r:=work(y);
for i:=l to l+2 do
for j:=r to r+2 do
t[i,j,c]:=false;
end;
procedure dfs(x,y:longint;c:char);
var
i,j,l,r,k:longint;
ww:array[0..1001,1..2] of longint;
cc:char;
keyy:boolean;
begin
map[x,y]:=c;
k:=0;
//t[x,y,cc]:=false;
for i:=1 to n do
begin
if (t[x,i,c]=true) and (map[x,i]='0') then begin
inc(k);
ww[k,1]:=x;
ww[k,2]:=i;
t[x,i,c]:=false;
end;
if (t[i,y,c]=true) and (map[i,y]='0') then begin
inc(k);
ww[k,1]:=i;
ww[k,2]:=y;
t[i,y,c]:=false;
end;
end;
l:=work(x);
r:=work(y);
for i:=l to l+2 do
for j:=r to r+2 do
if (t[i,j,c]=true) and (map[i,j]='0') then begin
inc(k);
ww[k,1]:=i;
ww[k,2]:=j;
t[i,j,c]:=false;
end;
if not(check) and (bii=false) then
begin
for i:=1 to n do
for j:=1 to n do
if map[i,j]='0' then
begin
keyy:=false;
for cc:='1' to '9' do
if t[i,j,cc] then begin
keyy:=true;
dfs(i,j,cc);
keyy:=false;
end;
if keyy=false then begin
map[x,y]:='0';
for ii:=1 to k do
t[ww[ii,1],ww[ii,2],c]:=true;
exit;
end;
end;
end
else if bii=false then
begin for i:=1 to n do
begin
for j:=1 to n do
write(map[i,j]);
writeln;
end;
bii:=true;
end;
map[x,y]:='0';
for i:=1 to k do
t[ww[i,1],ww[i,2],c]:=true;
//t[x,y,cc]:=true;
end;
begin
readln(kkk);
for iii:=1 to kkk do
begin
fillchar(map,sizeof(map),0);
n:=9;
key:=false;
tt:=true;
for i:=1 to n do
begin
readln(s);
for j:=1 to n do
map[i,j]:=s[j];
end; fillchar(t,sizeof(t),true);
for i:=1 to n do
for j:=1 to n do
if (map[i,j]='0') then begin
for ii:=1 to n do
begin
if map[i,ii]<>'0' then t[i,j,map[i,ii]]:=false;
if map[ii,j]<>'0' then t[i,j,map[ii,j]]:=false;
end;
l:=work(i);
r:=work(j);
for ii:=l to l+2 do
for jj:=r to r+2 do
if map[ii,jj]<>'0' then t[i,j,map[ii,jj]]:=false;
end;
while (tt) and (not(check)) do
begin
tt:=false;
for i:=1 to n do //hang
begin
fillchar(tot,sizeof(tot),0);
for j:=1 to n do
for cc:='1' to '9' do
if (map[i,j]='.') and (t[i,j,cc]) then inc(tot[cc]);
for cc:='1' to '9' do
if tot[cc]=1 then begin
tt:=true;
for j:=1 to n do
if (map[i,j]='0') and (t[i,j,cc]) then fill(i,j,cc);
end;
end;
for i:=1 to n do //lie
begin
fillchar(tot,sizeof(tot),0);
for j:=1 to n do
for cc:='1' to '9' do
if (map[j,i]='.') and (t[j,i,cc]) then inc(tot[cc]);
for cc:='1' to '9' do
if tot[cc]=1 then begin
tt:=true;
for j:=1 to n do
if (map[j,i]='0') and (t[j,i,cc]) then fill(j,i,cc);
end;
end;
for i:=1 to n do //4*4
for j:=1 to n do
if map[i,j]='0' then
begin
fillchar(tot,sizeof(tot),0);
l:=work(i);
r:=work(j);
for ii:=l to l+2 do
for jj:=r to r+2 do
for cc:='1' to '9' do
if (map[ii,jj]='0') and (t[ii,jj,cc]) then inc(tot[cc]);
for cc:='1' to '9' do
if tot[cc]=1 then begin
tt:=true;
for ii:=l to l+2 do
for jj:=r to r+2 do
if (map[ii,jj]='0')and(t[ii,jj,cc]) then fill(ii,jj,cc);
end;
end;
for i:=1 to n do
for j:=1 to n do
if map[i,j]='0' then
begin
sl:=0;
for cc:='1' to '9' do
if t[i,j,cc] then inc(sl);
if sl=1 then for cc:='1' to '9' do
if t[i,j,cc] then begin
tt:=true;
fill(i,j,cc);
end;
end;
end;
bii:=false;
for i:=1 to n do
for j:=1 to n do
if map[i,j]='0' then
for cc:='1' to '9' do
if t[i,j,cc] then dfs(i,j,cc);
if bii=false then
for i:=1 to n do
begin
for j:=1 to n do
write(map[i,j]);
writeln;
end;
end;
end.