program bn;
{ test the block numbering scheme
  04 Feb 1987   A.Herrmann }

const MAXSIZE=64;

var l0, x, y:   integer;
    i,j,k,m,n:  integer;
    block: array[1..MAXSIZE,1..MAXSIZE] of integer;

function fx(i:integer):integer;
begin
fx:= ((i+1) div 2) mod 2;
{fx:= i mod 2;}
end;

function fy(i:integer):integer;
begin
fy:= (i div 2) mod 2;
end;

function power(b,e:integer):integer;
var i,p:integer;
begin
i:=e; p:=1;
while i>0 do
    begin p:=p*b; i:=i-1; end;
power:=p;
end;

begin { bn }

write('Input start level '); readln(l0);
n:=power(4,l0);
writeln(n,' blocks');

for k:=1 to n do
    begin
    x:=1; y:=1;
    for i:=0 to l0-1 do
        begin
        j:=(k-1) div power(4,i);
        m:=power(2,i);
        x:=x+fx(j)*m;
        y:=y+fy(j)*m;
        end;
    block[x,y]:=k;
{    writeln(k:3,x:4,y:3);}
    end;
n:=power(2,l0);
for y:=1 to n do
    begin
    for x:=1 to n do
        write(block[x,y]:4);
    writeln;
    end;

end. { bn }
