这题是我好久以前打的了,感觉当时打的烂啊
暴力dfs,但是需要倒着枚举才可以⁞⁞⁞⁞꒰ ´╥ д ╥` ू ꒱⁞⁞⁞⁞
const
n=9;
fu: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));
fen:array[1..9,1..9] of longint=((6,6,6,6,6,6,6,6,6),
(6,7,7,7,7,7,7,7,6),
(6,7,8,8,8,8,8,7,6),
(6,7,8,9,9,9,8,7,6),
(6,7,8,9,10,9,8,7,6),
(6,7,8,9,9,9,8,7,6),
(6,7,8,8,8,8,8,7,6),
(6,7,7,7,7,7,7,7,6),
(6,6,6,6,6,6,6,6,6));
type
arr=array[1..9,1..9] of longint;
arr2=array[1..9,1..9] of boolean;
var
a:arr;
s,h,g:arr2;
i,j,k,ans:longint;
procedure dg(x,y,max:longint);
var
i:longint;
begin
if (x=0) and (y=9) then
begin
if max>ans then ans:=max;
exit;
end;
if a[x,y]<>0 then
begin
if y>1 then dg(x,y-1,max+a[x,y]*fen[x,y])
else dg(x-1,9,max+a[x,y]*fen[x,y]);
exit;
end;
for i:=1 to 9 do
if (h[x,i]=false) and (s[y,i]=false) and (g[fu[x,y],i]=false) then
begin
h[x,i]:=true;
s[y,i]:=true;
g[fu[x,y],i]:=true;
a[x,y]:=i;
if y>1 then dg(x,y-1,max+a[x,y]*fen[x,y])
else dg(x-1,9,max+a[x,y]*fen[x,y]);
h[x,i]:=false;
s[y,i]:=false;
g[fu[x,y],i]:=false;
a[x,y]:=0;
end;
end;
begin
assign(input,'sudoku.in');reset(input);
assign(output,'sudoku.out');rewrite(output);
for i:=1 to n do
for j:=1 to n do
begin
read(a[i,j]);
if a[i,j]<>0 then
begin
h[i,a[i,j]]:=true;
s[j,a[i,j]]:=true;
g[fu[i,j],a[i,j]]:=true;
end;
end;
ans:=0;
dg(9,9,0);
if ans=0 then writeln(-1)
else writeln(ans);
close(input);
close(output);
end.