unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, ExtCtrls, Buttons, Spin;
type
TForm1 =
class
(TForm)
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
PaintBox1: TPaintBox;
Button1: TButton;
SpinEdit1: TSpinEdit;
StaticText1: TStaticText;
Button2: TButton;
Memo1: TMemo;
procedure
FormCreate(Sender: TObject);
procedure
BitBtn1Click(Sender: TObject);
procedure
Button1Click(Sender: TObject);
procedure
FormClose(Sender: TObject;
var
Action: TCloseAction);
procedure
PaintBox1Paint(Sender: TObject);
procedure
Button2Click(Sender: TObject);
private
public
end
;
type
TMatrix=
array
[
0..100
,
0..100
]
of
Integer
;
var
Form1: TForm1;
MaxN:
Integer
;
Grath,Path,p:TMatrix;
Bmp:TBitMap;
implementation
{$R *.dfm}
procedure
TForm1
.
FormCreate(Sender: TObject);
begin
StringGrid1
.
ColCount:=
4
;
StringGrid1
.
RowCount:=
4
;
StringGrid1
.
DefaultColWidth:=
35
;
StringGrid1
.
DefaultRowHeight:=
35
;
Bmp:=TBitmap
.
Create();
Randomize();
end
;
procedure
TForm1
.
BitBtn1Click(Sender: TObject);
var
i,j:
Integer
;
begin
MaxN:=SpinEdit1
.
Value;
StringGrid1
.
ColCount:=MaxN
;
StringGrid1
.
RowCount:=MaxN
;
for
i:=
0
to
MaxN-
1
do
for
j:=
0
to
MaxN-
1
do
StringGrid1
.
Cells[i,j]:=
'0'
;
end
;
procedure
TForm1
.
Button1Click(Sender: TObject);
var
u,v,c,k, i,j:
Integer
;
X,Y:
array
of
Integer
;
begin
MaxN:=SpinEdit1
.
Value;
SetLength(X,MaxN);
SetLength(Y,MaxN);
for
i:=
0
to
MaxN-
1
do
for
j:=
0
to
MaxN-
1
do
if
StringGrid1
.
Cells[i,j]<>
'0'
then
StringGrid1
.
Cells[i,j]:=
'1'
;
if
i=j
then
StringGrid1
.
Cells[i,j]:=
'0'
;
for
i:=
0
to
MaxN-
1
do
for
j:=
0
to
MaxN-
1
do
begin
Grath[i,j]:=StrToInt(StringGrid1
.
Cells[i,j]);
Path[i,j]:=Grath[i,j];
end
;
with
Bmp
do
begin
Height:=PaintBox1
.
Height;
Width:=PaintBox1
.
Width;
Canvas
.
Brush
.
Color:=clWhite;
Canvas
.
Pen
.
Color:=clBlack;
Canvas
.
FillRect(Rect(
0
,
0
,Width,Height));
end
;
for
i:=
0
to
MaxN-
1
do
begin
X[i]:=Random(Bmp
.
Width);
Y[i]:=Random(Bmp
.
Height);
end
;
for
i:=
0
to
MaxN-
1
do
for
j:=
0
to
MaxN-
1
do
begin
if
Grath[i,j]=
1
then
begin
with
Bmp
do
begin
Canvas
.
Pen
.
Color:=clBlack;
Canvas
.
MoveTo(X[i],Y[i]);
Canvas
.
LineTo(X[j],Y[j]);
end
;
end
;
end
;
for
i:=
0
to
MaxN-
1
do
begin
with
Bmp
do
begin
Canvas
.
Brush
.
Color:=RGB(
192
,
192
,
192
);
Canvas
.
Ellipse(X[i]-
15
, Y[i]-
15
, X[i]+
15
, Y[i]+
15
);
Canvas
.
TextOut(X[i]-
3
, Y[i]-
8
, IntToStr(i+
1
));
end
;
end
;
PaintBox1
.
Invalidate();
for
k:=
0
to
MaxN-
1
do
for
u:=
0
to
MaxN-
1
do
for
v:=
0
to
MaxN-
1
do
if
(Path[u,k]+Path[k,v] < Path[u,v])
then
begin
Path[u,v]:=Path[u,k]+Path[k,v];
p[u,v]:=p[u,k];
end
;
for
u:=
0
to
MaxN-
1
do
for
v:=
0
to
MaxN-
1
do
StringGrid1
.
Cells[u,v]:=IntToStr(p[v,u]);
u:=
2
;
v:=
4
;
c:=u;
while
u<>v
do
begin
for
k:=
0
to
15
do
begin
if
Path[u,k]=Path[k,v]
then
Form1
.
Memo1
.
Text:=IntToStr(u)+
''
;
u:=k;
Break;
end
;
end
;
Form1
.
Memo1
.
Text:=IntToStr(v)+
''
;
end
;
procedure
TForm1
.
FormClose(Sender: TObject;
var
Action: TCloseAction);
begin
Bmp
.
Free();
end
;
procedure
TForm1
.
PaintBox1Paint(Sender: TObject);
begin
PaintBox1
.
Canvas
.
Draw(
0
,
0
,Bmp);
end
;
procedure
TForm1
.
Button2Click(Sender: TObject);
var
i,j:
Integer
;
begin
MaxN:=SpinEdit1
.
Value;
for
i:=
0
to
MaxN-
1
do
for
j:=
0
to
MaxN-
1
do
StringGrid1
.
Cells[i,j]:=IntToStr(Random(
2
));
for
i:=
0
to
MaxN-
1
do
StringGrid1
.
Cells[i,i]:=
'0'
;
end
;
end
.