【NOIP2009TG】靶形数独

35 篇文章 0 订阅
16 篇文章 0 订阅

这题是我好久以前打的了,感觉当时打的烂啊
暴力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.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值