Дано N. Вывести квадрат NxN клеток, заполненный по спирали и змейкой — Pascal(Паскаль)

program Zmeyka, Spiral;
 
type
  TCountMatr = 1..100;
 
const
  x = 0;
  y = 1;
 
type
  TCount = Low(TCountMatr)*Low(TCountMatr)..High(TCountMatr)*High(TCountMatr);
 
  TVect = array [TCountMatr] of Integer;
  TMatr = array [TCountMatr] of TVect;
 
  TNapravlenie1 = (n1Pravo, n1Niz, n1Levo, n1Verh);
  TNapravlenie2 = (n2PravoVerh, n2LevoNiz, n2Pravo, n2Niz);
 
  TVector = array [x..y] of Integer;
 
const
  VECTOR_1: array [TNapravlenie1] of TVector = ((1, 0), (0, 1), (-1, 0), (0, -1));
  VECTOR_2: array [TNapravlenie2] of TVector = ((1, -1), (-1, 1), (1, 0), (0, 1));
 
var
  RowCount, ColCount: TCountMatr;
  Row, Col: TCountMatr;
  Count: TCount;
  i, j: Integer;
  M1, M2: TMatr;
  N1: TNapravlenie1;
  N2: TNapravlenie2;
  MaxLen: Integer;
  sCount: String;
 
procedure NextNapravlenie1;
begin
  if N1 = High (TNapravlenie1) then N1 := Low (TNapravlenie1)
  else Inc (N1);
end;
 
procedure NextNapravlenie2;
begin
  case N2 of
    n2PravoVerh: begin
      if Col = ColCount then N2 := n2Niz
      else
        if Row = Low(Row) then N2 := n2Pravo;
    end;
    n2LevoNiz: begin
      if Row = RowCount then N2 := n2Pravo
      else
        if Col = Low(Col) then N2 := n2Niz;
    end;
    n2Pravo: begin
      if Row = RowCount then N2 := n2PravoVerh
      else N2 := n2LevoNiz;
    end;
    n2Niz: begin
      if Col = ColCount then N2 := n2LevoNiz
      else N2 := n2PravoVerh;
    end;
  end;
end;
 
function NextRow1: TCountMatr;
begin
  NextRow1 := Row + VECTOR_1 [N1, y];
end;
 
function NextCol1: TCountMatr;
begin
  NextCol1 := Col + VECTOR_1 [N1, x];
end;
 
function NextRow2: TCountMatr;
begin
  NextRow2 := Row + VECTOR_2 [N2, y];
end;
 
function NextCol2: TCountMatr;
begin
  NextCol2 := Col + VECTOR_2 [N2, x];
end;
 
begin
  write ('Kol-vo strok i stolbu,oB: ');
  readln (RowCount);
  ColCount:=RowCount;
 
  for i := Low(RowCount) to High(RowCount) do
    for j := Low(ColCount) to High(ColCount) do begin
      M1 [j, i] := 0;
      M2 [j, i] := 0;
    end;
 
  Count := RowCount * ColCount;
 
  Str (Count, sCount);
  MaxLen := Length (sCount);
 
  Row := Low (Row);
  Col := Low (Col);
  N1 := Low (N1);
  for i := Low (TCount) to Count do begin
    M1 [Col, Row] := i;
    if (NextRow1 < Low (RowCount)) or (NextRow1 > RowCount) or
       (NextCol1 < Low (ColCount)) or (NextCol1 > ColCount) or
       (M1 [NextCol1, NextRow1] <> 0) then begin
      NextNapravlenie1;
    end;
    Row := NextRow1;
    Col := NextCol1;
  end;
 
  Row := Low (Row);
  Col := Low (Col);
  N2 := Low (N2);
  for i := Low (TCount) to Count do begin
    M2 [Col, Row] := i;
    NextNapravlenie2;
    Row := NextRow2;
    Col := NextCol2;
  end;
 
  writeln ('M1');
  for i := Low(RowCount) to RowCount do begin
    for j := Low(ColCount) to ColCount do begin
      write (M1 [j, i]:MaxLen, ' ');
    end;
    writeln;
  end;
 
  writeln;
  writeln ('M2');
  for i := Low(RowCount) to RowCount do begin
    for j := Low(ColCount) to ColCount do begin
      write (M2 [j, i]:MaxLen, ' ');
    end;
    writeln;
  end;
 
  readln;
end.

Leave a Comment

+ 39 = 42