1 条题解

  • 0
    @ 2021-6-15 1:39:52

    Pascal :

    program sudoku;
      var usei,usej,usex:array[0..10,0..10] of boolean;
          usep:array[0..100] of boolean;
          maxi,a:array[0..10,0..10] of longint;
          t,s,max,tot,i,j,k,n,m,p:longint;
          x,y:array[0..100] of longint;
      function solve(i,J:longint):longint;
        var s,s1:longint;
      begin
        if (i<=j) then s:=i else s:=j;
        if (10-i<=10-j) then s1:=10-i else s1:=10-j;
        if (s=1) or (s1=1) then begin solve:=6; exit;end;
        if (s=2) or (s1=2) then begin solve:=7; exit;end;
        if (s=3) or (s1=3) then begin solve:=8; exit;end;
        if (s=4) or (s1=4) then begin solve:=9; exit;end;
        if (s=5) or (s1=5) then begin solve:=10;exit;end;
      end;
      function pr(i,j:longint):longint;
      begin
        pr:=(i-1) div 3*3+(j-1) div 3+1;
      end;
      procedure tryit(pp,now:longint);
        var t,min,w,j:longint;
      begin
        if pp=s+1 then
          begin
          if now>max then
            begin
            max:=now;
            maxi:=a;
          end
          end
          else
            begin
            t:=0; min:=999999;
            for i:=1 to s do
              if (a[x[i],y[i]]=0) and (not(usep[i])) then
                begin
                w:=0;
                for j:=1 to 9 do
                  if (usei[x[i],j]) and (usej[y[i],j]) and (usex[pr(x[i],y[i]),j]) then
                    begin
                    inc(w);
                    if w>=min then break;
                  end;
                if w<min then
                  begin
                  min:=w;
                  t:=i;
                end;
              end;
            if min=0 then exit;
            usep[t]:=true;
            for j:=1 to 9 do
              if (usei[x[t],j]) and (usej[y[t],j]) and (usex[pr(x[t],y[t]),j]) then
                begin
                usei[x[t],j]:=false;
                usej[y[t],j]:=false;
                usex[pr(x[t],y[t]),j]:=false;
                a[x[t],y[t]]:=j;
                tryit(pp+1,now+solve(x[t],y[t])*j);
                a[x[t],y[t]]:=0;
                usei[x[t],j]:=true;
                usej[y[t],j]:=true;
                usex[pr(x[t],y[t]),j]:=true;
              end;
            usep[t]:=false;
          end;
      end;
    begin
      fillchar(usei,sizeof(usei),true);
      fillchar(usej,sizeof(usej),true);
      fillchar(usex,sizeof(usex),true);
      tot:=0; max:=0; s:=0;
      for i:=1 to 9 do
        begin
        for j:=1 to 9 do
          begin
          read(a[i,j]);
          if a[i,j]<>0 then
            begin
            usei[i,a[i,j]]:=false;
            usej[j,a[i,j]]:=false;
            usex[pr(i,j),a[i,j]]:=false;
            t:=solve(i,j);
            tot:=tot+t*a[i,j];
          end;
          if (a[i,j]=0) then
            begin
            inc(s);
            x[s]:=i; y[s]:=j;
          end;
        end;
        readln;
      end;
      fillchar(usep,sizeof(usep),false);
      tryit(1,tot);
      if max=0 then writeln('-1') else writeln(max);
    end.
    
    
    • 1

    信息

    ID
    275
    时间
    2000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者