比赛 20100927 评测结果 C
题目名称 魔术数字游戏 最终得分 0
用户昵称 Achilles 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-09-27 21:58:20
显示代码纯文本
{$R+}
program magic;
var
  a,b,n3,n4,n5,i1,i2,p:longint;
  n1,n2:array[1..4]of longint;
  hx:array[-100..100]of boolean;
  sz:array[1..4,1..4]of longint;
procedure find(n:longint);
var
  i,a,b:longint;
begin
  if n>16 then begin
    for i1:=1 to 4 do
    begin
      for i2:=1 to 4 do
        write(sz[i1,i2]);
      writeln;
    end;
    writeln;
  end
  else begin
    b:=n mod 4;
    a:=n div 4;
    if b=0 then b:=4 else a:=a+1;
    if sz[a,b]=0 then begin
      if (a=4)and(b=1) then begin
        if (n1[1]=n4)and(hx[34-n4]) then begin
          sz[a,b]:=34-n4;
          hx[sz[a,b]]:=false;
          n1[4]:=n1[4]+34-n4;
          find(n+1);
          n1[4]:=n1[4]-(34-n4);
          hx[sz[a,b]]:=true;
          sz[a,b]:=0;
        end;
      end
      else begin
        if (a=4)and(b=4) then begin
          if (n1[4]=n3)and(hx[34-n3]) then begin
            sz[a,b]:=34-n3;
            hx[sz[a,b]]:=false;
            find(n+1);
            hx[sz[a,b]]:=true;
            sz[a,b]:=0;
          end;
        end
        else begin
          if (a=3)and(b=3) then begin
            if hx[34-n5] then begin
              sz[a,b]:=34-n5;
              hx[sz[a,b]]:=false;
              n3:=n3+i;
              n1[3]:=n1[3]+34-n5;
              n2[3]:=n2[3]+34-n5;
              find(n+1);
              n3:=n3-i;
              n1[3]:=n1[3]-(34-n5);
              n2[3]:=n2[3]-(34-n5);
              hx[sz[a,b]]:=true;
              sz[a,b]:=0;
            end;
          end
          else begin
            if a=4 then begin
              if hx[34-n1[b]] then begin
                sz[a,b]:=34-n1[b];
                hx[sz[a,b]]:=false;
                n2[4]:=n2[4]+34-n1[b];
                find(n+1);
                n2[4]:=n2[4]-(34-n1[b]);
                hx[sz[a,b]]:=true;
                sz[a,b]:=0;
              end;
            end
            else begin
              if b=4 then begin
                if hx[34-n2[a]] then begin
                  sz[a,b]:=34-n2[a];
                  hx[sz[a,b]]:=false;
                  n1[4]:=n1[4]+34-n2[a];
                  find(n+1);
                  n1[4]:=n1[4]-(34-n2[a]);
                  hx[sz[a,b]]:=true;
                  sz[a,b]:=0;
                end;
              end
              else begin
                for i:=1 to 16 do
                begin
                  if hx[i] then begin
                    sz[a,b]:=i;
                    hx[i]:=false;
                    if (a=4)and(b=1) then begin
                      n1[1]:=n1[1]+i;
                      n4:=n4+i;
                    end;
                    if (a=4)and(b=4) then begin
                      n1[4]:=n1[4]+i;
                      n2[4]:=n2[4]+i;
                      n3:=n3+i;
                    end;
                    if (a=3)and(b=3) then begin
                      n5:=n5+i;
                    end;
                    if (not((a=4)and(b=1)))and(not((a=4)and(b=4))) then begin
                      n1[b]:=n1[b]+i;
                      n2[a]:=n2[a]+i;
                      if a=b then n3:=n3+i;
                      if a=(5-b) then n4:=n4+i;
                    end;
                    if (n1[b]<=34)and(n2[b]<=34)and(n3<34)and(n4<34)and(n5<34) then find(n+1);
                    if (a=4)and(b=1) then begin
                      n1[1]:=n1[1]-i;
                      n4:=n4-i;
                    end;
                    if (a=4)and(b=4) then begin
                      n1[4]:=n1[4]-i;
                      n2[4]:=n2[4]-i;
                      n3:=n3-i;
                    end;
                    if (a=3)and(b=3) then begin
                      n5:=n5-i;
                    end;
                    if (not((a=4)and(b=1)))and(not((a=4)and(b=4))) then begin
                      n1[b]:=n1[b]-i;
                      n2[a]:=n2[a]-i;
                      if a=b then n3:=n3-i;
                      if a=(5-b) then n4:=n4-i;
                    end;
                    hx[i]:=true;
                    sz[a,b]:=0;
                  end;
                end;
              end;
            end;
          end;
        end;
      end;
    end
    else begin
      p:=0;
      if (a=4)and(n1[b]<>34) then p:=1;
      if (b=4)and(n2[a]<>34) then p:=1;
      if (a=4)and(b=4)and(n3<>34) then p:=1;
      if (a=4)and(b=1)and(n4<>34) then p:=1;
      if (a=3)and(b=3)and(n5<>34) then p:=1;
      if p=0 then find(n+1);
    end;
  end;
end;
begin
  assign(input,'magic.in');
  assign(output,'magic.out');
  reset(input);
  rewrite(output);
  readln(a,b);
  fillchar(sz,sizeof(sz),0);
  sz[a,b]:=1;
  fillchar(n1,sizeof(n1),0);
  fillchar(n2,sizeof(n2),0);
  n3:=0;
  n4:=0;
  n5:=0;
  n1[b]:=1;
  n2[a]:=1;
  if a=b then n3:=1;
  if a=5-b then n4:=1;
  if (2<=a)and(a<=3)and(2<=b)and(b<=3) then n5:=1;
  fillchar(hx,sizeof(hx),false);
  for i1:=2 to 16 do
    hx[i1]:=true;
  find(1);
  close(input);
  close(output);
end.