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.
    
    转载需注明出处。
  • 相关阅读:
    数据结构第九篇——栈与递归
    c++重载(以运算符重载为主)
    (五)分数阶微分方程的解法及其适定性问题介绍
    (四)分数阶微积分
    (三)分数阶微积分
    (二)分数阶微积分
    小学教育试讲
    高中教育试讲
    【级数】 求和
    题东湖风光村
  • 原文地址:https://www.cnblogs.com/jz929/p/11817794.html
Copyright © 2011-2022 走看看