zoukankan      html  css  js  c++  java
  • 【NOIP2009TG】靶形数独

    这题是我好久以前打的了,感觉当时打的烂啊
    暴力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.
    
    转载需注明出处。
  • 相关阅读:
    retofit、ButterKnite
    [iOS笔记]Swift中的Optional类型
    [iOS笔记] 网络通信安全与证书
    [iOS]ReactiveCocoa playground配置
    NAT学习笔记
    [iOS学习笔记]runloop runMode方法调研
    Python 装饰器简单介绍
    cookie实现自动登陆原理
    iOS开发添加Reveal工具
    【iOS Programming: The Big Nerd Ranch Guide】【笔记】2
  • 原文地址:https://www.cnblogs.com/jz929/p/11817794.html
Copyright © 2011-2022 走看看