不是VIP会员,不能显示答案

题目解答

题目:
(螺旋矩阵) 一个n行m列的螺旋矩阵可由如下方法生成:从矩阵的左上角(第1行第1列)出发,初始时向右移动;如果前方是未曾经过的格子,则继续前进,否则右转;重复上述操作直至经过矩阵中所有格子。根据经过顺序,在格子中依次填入1, 2, 3, ... , n^2,便构成了一个螺旋矩阵。输入n和m,输出一个螺旋矩阵,同一行中相邻两个数字中间有一个空格,行末没有空格。
var 
    n, m, i, j, t, s:longint; 
    a:array[-1..11,-1..11] of longint; 
    dx:array[1..4] of longint=(-1,0,0,1); 
    dy:array[1..4] of longint=(0,-1,1,0); 
begin 
    readln(n,m); 
i:=1;    j:=1;     
t:=1;    s:=1;  //t表示当前要填的数值s表示当前填的方向 
    a[i,j]:=t; 
    while     t<n*m      do begin 
        inc(t); 
        i:=i+dx[s]; 
        j:=      j+dy[s]      ; 
        if (i>n) or (i<1) or (    j>m   ) or (j<1) or (a[i,j]<>0) then  
          begin 
            i:=i-dx[s]; 
            j:=j-dy[s]; 
            dec(t); 
            s:=s+1; 
            if s=5 then       s:=1      ; 
            continue; 
        end; 
        a[i,j]:=t; 
    end; 
for i:=1 to n do  
begin 
        for j:=1 to m-1 do write(a[i,j],' '); 
        writeln(      a[i,m]      ); 
    end; 
end. 
考点:
分析:
解答:
评论:
老师: