Доброго времени суток! очень нужна ваша помощь. Помогите решить задачу.
Начиная с центра. обойти по спирали все элементы квадратной матрицы размером 13х13 (распечатывая их в порядке обхода).
Заранее огромное спасибо!
for i:=1 to yl do
begin
for j:=1 to xl do
begin
a1[j,i]:=(i-1)*xl+j;
a2[j,i]:=false;
end;
end;
for i:=1 to yl do
begin
for j:=1 to xl do
begin
read(a1[j,i]);
a2[j,i]:=false;
end;
end;
x:=1;
y:=1;
dir:=0;
posl[1]:=a1[(xl div 2)+1,(yl div 2)+1];
i:=xl*yl;
repeat
a2[x,y]:=true;
posl[i]:=a1[x,y];
dec(i);
CS:case dir of
0:if (x=xl) or a2[x+1,y] then
begin
dir:=1;
goto CS;
end
else inc(x);
1:if (y=yl) or a2[x,y+1] then
begin
dir:=2;
goto CS;
end
else inc(y);
2:if (x=1) or a2[x-1,y] then
begin
dir:=3;
goto CS;
end
else dec(x);
3:if (y=0) or a2[x,y-1] then
begin
dir:=0;
goto CS;
end
else dec(y);
end;
until (y=(yl div 2)+1) and (x=(xl div 2)+1);
const
size = 13;
var
X, Y: integer;
square: array[1 .. size, 1 .. size] of integer;
function check(X, Y: integer): boolean;
begin
check := ((X > 0) and (X <= size) and
(Y > 0) and (Y <= size))
end;
type
dirs = (_up, _down, _left, _right);
axis = (_X, _Y);
const
offset: array[dirs, axis] of integer = (
(0, -1), (0, 1), (1, 0), (-1, 0)
);
function go(where: dirs; delta: integer): boolean;
var i: integer;
begin
go := false;
for i := 1 to delta do
if check(Y + offset[where, _Y], X + offset[where, _X]) then begin
X := X + offset[where, _X];
Y := Y + offset[where, _Y];
write(square[Y, X]:4);
end
else exit;
go := true;
end;
var
i, j, plus: integer;
status: boolean;
begin
for i := 1 to size do
for j := 1 to size do
square[i, j] := (i - 1)*size + j;
for i := 1 to size do begin
for j := 1 to size do write(square[i, j]:4);
writeln;
end;
plus := 0;
X := size div 2 + 1; Y := size div 2 + 1;
write(square[Y, X]:4);
status := true;
repeat
inc(plus);
status := status and go(_right, plus);
status := status and go(_down, plus);
inc(plus);
status := status and go(_left, plus);
status := status and go(_up, plus);
until not status;
end.