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
{ Private declarations }
public
{ Public declarations }
end;
type
TMatrix=array {of 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{+1};
StringGrid1.RowCount:=MaxN{+1};
{ //нумерация
for i:=1 to MaxN do
begin
StringGrid1.Cells[i,0]:=IntToStr(i);
StringGrid1.Cells[0,i]:=IntToStr(i);
end; }
for i:=0 to MaxN-1 do
for j:=0 to MaxN-1 do
StringGrid1.Cells[i,j]:='0';
end;
{procedure TForm1Drawing(var Grath:TMatrix;const MaxN:Integer; PaintBox:TPaintBox);
var
i,j:Integer;
begin
with PaintBox do
begin
PaintBox.Canvas.Brush.Color:=clWhite;
PaintBox.Canvas.Rectangle(0,0,PaintBox.Width,PaintBox.Height);
PaintBox.Canvas.Pen.Color:=clBlack;
PaintBox.Canvas.Pen.Width:=2;
end;
end; }
procedure TForm1.Button1Click(Sender: TObject);
var
u,v,c,k, i,j:Integer;
X,Y:array {[0..100] } of Integer;
begin
MaxN:=SpinEdit1.Value;
SetLength(X,MaxN);
SetLength(Y,MaxN);
// SetLength(Grath,MaxN,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';
// Grath[i,j]:=StrToInt(StringGrid1.Cells[i,j]);
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); {random(200)+50; }
Y[i]:=Random(Bmp.Height); {random(200)+50;}
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]; { p[i,j]:=k; }
end;
for u:=0 to MaxN-1 do
for v:=0 to MaxN-1 do
StringGrid1.Cells[u,v]:=IntToStr(p[v,u]); {IntToStr(p[j,i]); }
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)+'';
{ while c<>v do
begin
Form1.Memo1.Text:=(IntToStr(c)+'->');
c:=p[c,j];
Form1.Memo1.Text:=(IntToStr(v)+'->');
Dec(c);
end; }
{ while j>0 do
begin
Form1.Memo1.Text := IntToStr(j) + ' ' + Form1.Memo1.Text;
j:=p[i,j];
Dec(j);
end; }
//------------------------------------------------------------------------конец
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);
// PaintBox1.Invalidate();
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.