
Interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TSearch = class(TThread)
private
{ Private declarations }
procedure FindWay;
protected
procedure Execute; override;
end;
Implementation
uses Unit1;
{ TSearch }
var
Map : array [0..500,0..500] of Integer;
Count:integer;
BitMap : TBitmap;
procedure TSearch.Execute;
begin
{ Place thread code here }
Synchronize(FindWay);
end;
procedure TSearch.FindWay;
var
i,j,w,h:integer;
x,y,k,x1,y1,x2,y2:integer;
rec:TRect;
begin
Bitmap:=TBitmap.Create;
for i:=0 to Form1.N-1 do
for j:=0 to Form1.M-1 do
Map[i,j]:=0;
h:=Round(500/Form1.N);
w:=Round(500/Form1.M);
with Form1.Image1.Canvas do
begin
for i:=0 to Form1.N-1 do
for j:=0 to Form1.M-1 do
begin
X:=40+Trunc(h/2)+i*h;
Y:=5+Trunc(w/2)+j*w;
if Pixels[X,Y] = clSilver
then Map[i,j]:=-1
else Map[i,j]:=500;
end;
Map[Form1.N-1,Form1.M-1]:=0;
i:=Form1.N-1;
j:=Form1.M-1;
if Map[i-1,j] <> -1 then Map[i-1,j]:=1;
if Map[i,j-1] <> -1 then Map[i,j-1]:=1;
k:=1;
while k<=Round(Form1.N*Form1.M/2) do
begin
for i:=0 to Form1.N-1 do
for j:=0 to Form1.M-1 do
if Map[i,j]=k then
begin
if (i<=Form1.N-1)and(Map[i-1,j]<>-1)and(Map[i,j]<Map[i-1,j])
then Map[i-1,j]:=k+1;
if (i<Form1.N-1)and(Map[i+1,j]<>-1)and(Map[i,j]<Map[i+1,j])
then Map[i+1,j]:=k+1;
if (j<=Form1.M-1)and(Map[i,j-1]<>-1)and(Map[i,j]<Map[i,j-1])
then Map[i,j-1]:=k+1;
if (j<Form1.M-1)and(Map[i,j+1]<>-1)and(Map[i,j]<Map[i,j+1])
then Map[i,j+1]:=k+1;
end;
Inc(k);
end;
//
Form1.Image1.Canvas.Brush.Style:=bsClear;
Form1.Image1.Canvas.Font.Height:=20;
Form1.Image1.Canvas.Font.Name:='Times New Roman';
Count:=0;
if (Map[0,0]=500)
then
begin
ShowMessage('Пройти лабиринт невозможно!');
end
else
begin
k:=Map[0,0];
i:=0;
j:=0;
while k>=0 do
begin
if (i>=0)and(Map[i+1,j]<>-1)and(Map[i,j]-Map[i+1,j] = 1) then
begin
x1:=41+(i)*h;
y1:=5+(j)*w;
x2:=h+39+(i)*h;
y2:=w+3+(j)*w;
rec:=Rect(x1,y1,x2,y2);
Form1.Image1.Canvas.Brush.Color:=($505045);
Form1.Image1.Canvas.FillRect(rec);
//
X:=40+Trunc(h/4)+i*h;
Y:=5+Trunc(w/4)+j*w;
Form1.Image1.Canvas.TextOut(X,Y,IntToStr(Count));
Inc(Count);
//
Inc(i);
end;
//
if (i>0)and(Map[i-1,j]<>-1)and(Map[i,j]-Map[i-1,j] = 1) then
begin
x1:=41+(i)*h;
y1:=5+(j)*w;
x2:=h+39+(i)*h;
y2:=w+3+(j)*w;
rec:=Rect(x1,y1,x2,y2);
Form1.Image1.Canvas.Brush.Color:=($505045);
Form1.Image1.Canvas.FillRect(rec);
X:=40+Trunc(h/4)+i*h;
Y:=5+Trunc(w/4)+j*w;
Form1.Image1.Canvas.TextOut(X,Y,IntToStr(Count));