unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 =
class
(TForm)
StringGrid1: TStringGrid;
Button1: TButton;
Label1: TLabel;
procedure
Button1Click(Sender: TObject);
private
public
end
;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
i:
integer
;
begin
for
i:=l
to
10
do
StringGrid1. Cells [
0
,i] :=IntToStr (i) ;
for
i:=l
to
10
do
StringGridl
.
Cells[i,
0
]:=IntToStr (i);
StringGrid1
.
Cells[
1
,
2
]:=
'1'
;
StringGrid1
.
Cells[
2
,
1
]:=
'1'
;
StringGrid1
.
Cells[
1
,
3
]:=
'1'
;
StringGrid1
.
Cells[
3
,
1
]:=
'1'
;
StringGrid1
.
Cells[
1
,
4
]:=
'1'
;
StringGrid1
.
Cells[
4
,
1
]:=
'1'
;
StringGrid1
.
Cells[
3
,
7
]:=
'1'
;
StringGrid1
.
Cells[
7
,
3
]:=
'1'
;
StringGrid1
.
Cells[
4
,
6
]:=
'1'
;
StringGrid1
.
Cells[
6
,
4
]:=
'1'
;
StringGrid1
.
Cells[
5
,
6
]:=
'1'
;
StringGrid1
.
Cells[
6
,
5
]:=
'1'
;
StringGrid1
.
Cells[
5
,
7
]:=
'1'
;
StringGrid1
.
Cells(
7
,
5
]:=
'1'
;
StringGrid1
.
Cells[
6
,
7
]:=
'1'
;
StringGrid1
.
Cells[
7
,
6
]:=
'1'
;
end
.
begin
end
;
procedure
TForm1
.
ButtonlClick(Sender: TObject);
const
N=
10
;
var
map:
array
[
1..
N,
1..
N]
of
integer
;
road:
array
[
1..
N]
of
integer
;
incl:
array
[
1..
N]
of
boolean
;
start, finish:
integer
;
found:
boolean
; len:
integer
;
procedure
step(s,f,p:
integer
);
var
с:
integer
;
i:
integer
;
begin
if
s=f
then
begin
len:=c_len;
for
i:=
1
to
p-
1
do
Label1
.
caption:=Label1
.
caption+
' '
+IntToStr(road[i]);
Label1
.
caption:=Label1
.
caption
+
', длина:'
+IntToStr(len)+#
13
;
end
else
for
c:=l
to
N
do
if
(map[s,c]<>
0
)
and
(
NOT
incite])
and
((len=
0
)
or
(c_len+map[s,c]< len))
then
begin
roadtp]:=c;
incl[c]:=
TRUE
;
c_len:=c_len+map[s,с];
step(c,f,p+l);
incite]:=
FALSE
; roadtp]:=
0
;
c_len:=c_len-map[s,с];
end
;
end
;
begin
Labell
.
caption:=
''
;
for
i: =
1
to
N
do
road [ i ] : =
0
;
for
i:=l
to
N
do
incl[i]:=
FALSE
;
for
i:=l
to
N
do
for
j:=
1
to
N
do
if
StringGridl
.
Cells[i, j] <> "
then
mapti,j]:=StrToInt(StringGridl
.
Cells[i,j])
else
mapti,j]:=
0
;
len:=
0
;
len:=
0
,-
start:=StrToInt(Edit1
.
text);
finish:=StrToInt(Edit2
.
text);
road[
1
]:=start;
incl[start]:=
TRUE
;
step(start,finish,
2
);
if
not
found
then
Label1
.
caption:=
'Указанные точки не соединены!'
;
end
;
end
;
end
.