|
|

楼主 |
发表于 2006-2-12 16:07:00
|
显示全部楼层
Re:delphi数组问题(好象和数学有关)
N是任意数,
请问,下面我做的对不对哦?
-------------------------------
var
Datas:array of array of Integer;
procedure Fill2DArray(N, RowCount, ColCount: Integer);
type
TDir=(dirRight,dirDown,dirLeft,dirUp);
const
Dirs:array[TDir,0..1]of Integer=((0,1),(1,0),(0,-1),(-1,0));
var
i,x,y,x1,y1,c:Integer;
MyDir:TDir;
b:Boolean;
begin
if RowCount or ColCount=0 then exit;
MyDir:=dirRight;
for y:=0 to Pred(RowCount) do
for x:=0 to Pred(ColCount) do
Datas[y,x]:=0;
x:=0; y:=0;
i:=1;
while (i<=N) and (Datas[y,x]=0) do
begin
Datas[y,x]:=i;
c:=0;
while c<4 do
begin
x1:=x+Dirs[MyDir][1];
y1:=y+Dirs[MyDir][0];
if (x1>=0) and (y1>=0) and (x1<ColCount) and (y1<RowCount) then
begin
if Datas[y1,x1]=0 then
begin
x:=x1;
y:=y1;
break;
end
else
if MyDir=dirUp then
MyDir:=dirRight
else
MyDir:=Succ(MyDir);
end
else
if MyDir=dirUp then
MyDir:=dirRight
else
MyDir:=Succ(MyDir);
Inc(c);
end;
Inc(i);
end;
end;
使用例子:
SetLength(Datas,SpinEdit3.Value,SpinEdit2.Value);
Fill2DArray(SpinEdit1.Value,SpinEdit3.Value,SpinEdit2.Value); |
|