记录编号 19068 评测结果 AAAAAAAAAA
题目名称 魔术数字游戏 最终得分 100
用户昵称 Gravatarmaxiem 是否通过 通过
代码语言 Pascal 运行时间 0.172 s
提交时间 2010-09-28 14:08:50 内存使用 0.11 MiB
显示代码纯文本
program magic;
const
  s:array [1..6,1..2] of integer=((1,1),(1,2),(1,3),(2,1),(2,3),(3,1));
var
  flag:array [1..16] of boolean;
  table:array [1..4,1..4] of integer;
  a,b:integer;
procedure judge;
var i,j,t41,t32,t42,t33,t43,t44,t34:integer;
begin
  t41:=34-table[1,1]-table[2,1]-table[3,1];
  if (table[4,1]<>0) and (table[4,1]<>t41) then exit;
  if (t41>16) or (t41<1) then exit;
  if (flag[t41]) then if not((a=4) and (b=1)) then exit;
  t32:=34-table[1,4]-table[2,3]-t41;
  if t41=t32 then exit;
  if (table[3,2]<>0) and (table[3,2]<>t32) then exit;
  if (t32>16) or (t32<1) then exit;
  if (flag[t32]) then if not((a=3) and (b=2)) then exit;
  t42:=34-t41-t32-table[3,1];
  if (t42=t32) or (t42=t41) then exit;
  if t42<>34-table[1,2]-table[2,2]-t32 then exit;
  if (table[4,2]<>0) and (table[4,2]<>t42) then exit;
  if (t42>16) or (t42<1) then exit;
  if (flag[t42]) then if not((a=4) and (b=2)) then exit;
  t33:=34-table[2,2]-table[2,3]-t32;
  if (t33=t42) or (t33=t32) or (t33=t41) then exit;
  if (table[3,3]<>0) and (table[3,3]<>t33) then exit;
  if (t33>16) or (t33<1) then exit;
  if (flag[t33]) then if not((a=3) and (b=3)) then exit;
  t43:=34-table[1,3]-table[2,3]-t33;
  if (t43=t33) or (t43=t42) or (t43=t32) or (t43=t41) then exit;
  if (table[4,3]<>0) and (table[4,3]<>t43) then exit;
  if (t43>16) or (t43<1) then exit;
  if (flag[t43]) then if not((a=4) and (b=3)) then exit;
  t44:=34-t41-t42-t43;
  if (t44=t43) or (t44=t33) or (t44=t42) or (t44=t32) or (t44=t41) then exit;
  if t44<>34-table[1,1]-table[2,2]-t33 then exit;
  if (table[4,4]<>0) and (table[4,4]<>t44) then exit;
  if (t44>16) or (t44<1) then exit;
  if (flag[t44]) then if not((a=4) and (b=4)) then exit;
  t34:=34-table[1,4]-table[2,4]-t44;
  if (t34=t44) or (t34=t43) or (t34=t33) or (t34=t42) or (t34=t32) or (t34=t41) then exit;
  if t34<>34-table[3,1]-t32-t33 then exit;
  if t34<>34-t33-t43-t44 then exit;
  if (table[3,4]<>0) and (table[3,4]<>t34) then exit;
  if (t34>16) or (t34<1) then exit;
  if (flag[t34]) then if not((a=3) and (b=4)) then exit;
  if table[1,1]+table[1,4]+t41+t44<>34 then exit;
  table[4,1]:=t41;
  table[3,2]:=t32;
  table[4,2]:=t42;
  table[3,3]:=t33;
  table[4,3]:=t43;
  table[4,4]:=t44;
  table[3,4]:=t34;
  for i:=1 to 4 do begin
    write (table[i,1]);
	for j:=2 to 4 do write (' ',table[i,j]);
	writeln;
  end;
  writeln;
  table[4,1]:=0;
  table[3,2]:=0;
  table[4,2]:=0;
  table[3,3]:=0;
  table[4,3]:=0;
  table[4,4]:=0;
  table[3,4]:=0;
end;
procedure search(step:integer);
var tmp,tmp2,i:integer;
begin
  i:=2;
  if table[s[step,1],s[step,2]]<>0 then i:=16;
  while i<=16 do begin
    while (i<16) and (flag[i]=true) do inc(i);
    if (table[s[step,1],s[step,2]]=0) and (i<=16) then begin
      table[s[step,1],s[step,2]]:=i;
      flag[i]:=true;
    end;
    if i<=16 then begin
    case step of
    1,2:search(step+1);
    3:begin
	  tmp:=34-(table[1,1]+table[1,2]+table[1,3]);
	  if table[1,4]<>0 then begin
	    if table[1,4]=tmp then begin
		  search(step+1);
		end;
	  end
	  else begin
      if (tmp>=1) and (tmp<=16) and (flag[tmp]=false) then begin
		  table[1,4]:=tmp;
		  flag[tmp]:=true;
		  search(step+1);
		  flag[tmp]:=false;
		  table[1,4]:=0;
		end;
	  end;
	end;
	4:begin
	  tmp:=34-table[1,1]-table[1,2]-table[2,1];
	  if table[2,2]<>0 then begin
	    if table[2,2]=tmp then begin
		  search(step+1);
		end;
	  end
	  else begin
            if (tmp>=1) and (tmp<=16) and (flag[tmp]=false) then begin
		  table[2,2]:=tmp;
		  flag[tmp]:=true;
		  search(step+1);
		  flag[tmp]:=false;
		  table[2,2]:=0;
	  end;
	end;
	end;
	5:begin
	  tmp:=34-table[2,1]-table[2,2]-table[2,3];
	  tmp2:=34-table[1,3]-table[1,4]-table[2,3];
	  if tmp=tmp2 then begin
	    if table[2,4]<>0 then begin
	      if table[2,4]=tmp then begin
		    search(step+1);
		  end;
		end
		else begin
                  if (tmp>=1) and (tmp<=16) and (flag[tmp]=false) then begin
		    table[2,4]:=tmp;
			flag[tmp]:=true;
			search(step+1);
			flag[tmp]:=false;
			table[2,4]:=0;
		  end;
		end;
	  end;
	end;
	6:judge;
	end;
        end;
        if (i<=16) and ((s[step,1]<>a) or (s[step,2]<>b)) then begin
          flag[i]:=false;
          table[s[step,1],s[step,2]]:=0;
        end;
        inc(i);
  end;
end;
begin
  fillchar (flag,sizeof(flag),0);
  fillchar (table,sizeof(table),0);
  flag[1]:=true;
  assign (input,'magic.in');
  reset (input);
  readln (a,b);
  table[a,b]:=1;
  close (input);
  assign (output,'magic.out');
  rewrite (output);
  search(1);
  close (output);
end.