Cod sursa(job #228855)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 8 decembrie 2008 15:18:57
Problema Fractal Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.88 kb
var k,x,y:longint;
    f,g:text;

function rec(k,x,y:longint):longint;
 var t,zone,x0,y0:longint;
 begin
  t:=1 shl (k-1);
  if (x<=t) and (y<=t) then
   zone:=0;
  if (x<=t) and (y>t) then
   zone:=3;
  if (x>t) and (y<=t) then
   zone:=1;
  if (x>t) and (y>t) then
   zone:=2;
  if k=1 then begin
   rec:=zone;
   exit;
  end;
  x0:=x;
  y0:=y;
  case zone of
  0: begin
        y:=x0;
        x:=y0;
        rec:=rec(k-1,x,y);
  end;

  1: begin
      x:=x0-t;
      rec:=t*t+rec(k-1,x,y);
  end;

  2: begin
        x:=x0-t;
        y:=y0-t;
        rec:=t*t*2+rec(k-1,x,y);
  end;

  3: begin
        x:=2*t-y0+1;
        y:=t-x0+1;
        rec:=t*t*3+rec(k-1,x,y);
  end;

  end;

 end;

begin
 assign(f,'fractal.in'); reset(f);
 assign(g,'fractal.out'); rewrite(g);
 read(f,k,x,y);
 writeln(g,rec(k+1,x,y));
 close(f); close(g);
end.