program sierpinski(input,output);
const max = 128;
      x = 'x';
type board = array[1..max,1..max] of char;
var BD : board;
    i,num,row,col : integer;
procedure initialize(var BD : board);
var i,j : integer;
begin
  for i := 1 to max do
  for j := 1 to max do
    BD[i,j] := ' '
end;
procedure printboard(num : integer; BD : board);
var i,j : integer;
begin
writeln;
writeln('Sierpinski Curve ', num:2);
writeln;
for i:= 1 to max do
  begin
    for j := 1 to max do
	write(BD[i,j]);
    writeln
  end
end;
procedure drawright( var i,j : integer; var BD : board);
var col : integer;
begin
  for col := j to j+3 do
	BD[i,col] := x;
  j := j + 4
end;
procedure drawleft( var i,j : integer; var BD : board);
var col : integer;
begin
  for col := j-3 to j do
	BD[i,col] := x;
  j := j - 4
end;
procedure drawup( var i,j : integer; var BD : board);
var row : integer;
begin
  for row := i-3 to i do
	BD[row,j] := x;
  i := i - 4
end;
procedure drawdown( var i,j : integer; var BD : board);
var row : integer;
begin
  for row := i to i + 3 do
	BD[row,j] := x;
  i := i + 4
end;
procedure leftdiagonalup( var i,j : integer; var BD : board);
begin
  BD[i-1,j-1] := x;
  BD[i,j] := x;
  i := i - 2;
  j := j - 2
end;
procedure leftdiagonaldown( var i,j : integer; var BD : board);
begin
  BD[i+1,j+1] := x;
  BD[i,j] := x;
  i := i + 2;
  j := j + 2
end;
procedure rightdiagonalup( var i,j : integer; var BD : board);
begin
  BD[i-1,j+1] := x;
  BD[i,j] := x;
  i := i - 2;
  j := j + 2
end;
procedure rightdiagonaldown( var i,j : integer; var BD : board);
begin
  BD[i+1,j-1] := x;
  BD[i,j] := x;
  i := i + 2;
  j := j - 2
end;
procedure B(i : integer; var row,col : integer; var BD : board);forward;
procedure C(i : integer; var row,col : integer; var BD : board);forward;
procedure D(i : integer; var row,col : integer; var BD : board);forward;
procedure A(i : integer; var row,col : integer; var BD : board);
begin
  if i > 0 then
	begin
	  A(i-1,row,col,BD);
	  leftdiagonaldown(row,col,BD);
	  B(i-1,row,col,BD);
	  drawdown(row,col,BD);
	  D(i-1,row,col,BD);
	  rightdiagonaldown(row,col,BD);
	  A(i-1,row,col,BD)
	end
end;
procedure B;
begin
  if i > 0 then
	begin
	  B(i-1,row,col,BD);
	  rightdiagonalup(row,col,BD);
	  C(i-1,row,col,BD);
	  drawright(row,col,BD);
	  A(i-1,row,col,BD);
	  leftdiagonaldown(row,col,BD);
	  B(i-1,row,col,BD)
	end
end;
procedure C;
begin
  if i > 0 then
	begin
	  C(i-1,row,col,BD);
	  leftdiagonalup(row,col,BD);
	  D(i-1,row,col,BD);
	  drawup(row,col,BD);
	  B(i-1,row,col,BD);
	  rightdiagonalup(row,col,BD);
	  C(i-1,row,col,BD)
	end
end;
procedure D;
begin
  if i > 0 then
	begin
	  D(i-1,row,col,BD);
	  rightdiagonaldown(row,col,BD);
	  A(i-1,row,col,BD);
	  drawleft(row,col,BD);
	  C(i-1,row,col,BD);
	  leftdiagonalup(row,col,BD);
	  D(i-1,row,col,BD)
	end
end;
procedure S(i : integer; var row,col : integer; var BD : board);
begin
  A(i,row,col,BD);
  leftdiagonaldown(row,col,BD);
  B(i,row,col,BD);
  rightdiagonalup(row,col,BD);
  C(i,row,col,BD);
  leftdiagonalup(row,col,BD);
  D(i,row,col,BD);
  rightdiagonaldown(row,col,BD)
end;
begin
  write('Type in num : ');
  readln(num);
  initialize(BD);
  row := 4;
  col := 1;
  S(num,row,col,BD);
  printboard(num,BD)
end.