{Made by for NORMA4 users
     #####     #####    #####   #############
   ###   ###     ###     ##     ##       ###
  ###              ### ##              ###
  ###                ###             ###
  ###              ##  ###         ###
   ###   ###     ##      ###     ###       ##
     #####     #####     #####  ############
}
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls;

type
  TForm1 = class(TForm)
    Grid: TDrawGrid;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Button1: TButton;
    Button2: TButton;
    Grid1: TStringGrid;
    Button3: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure GridClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Puti;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
type
  Skletka=record
   num,val:integer;
  end;
var
  Form1: TForm1;
  x0,y0,x_,y_,x1,y1,x,y,i,j,Ni,Nk,x2,y2:integer;
  map:array[1..40,1..40]of byte;
  map_:array[1..40,1..40]of boolean;
  can:boolean;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 x0:=trunc(strtofloat(edit1.Text));
 y0:=trunc(strtofloat(edit2.Text));
 x_:=trunc(strtofloat(edit3.Text));
 y_:=trunc(strtofloat(edit4.Text));
 map[x0,y0]:=253;
 map[X_,y_]:=0;
 Nk:=253;
 Ni:=0;
 {for i:=1 to 40 do
  for j:=1 to 40 do
   if map[i,j]=Ni then
    begin
     can:=false;
     if map[i+1,j]=253 then can:=true;
     if (map[i+1,j]=254)and (map_[i+1,j]=false) then begin map_[i+1,j]:=true;map[i+1,j]:=Ni+1;end;
     if map[i-1,j]=253 then can:=true;
     if (map[i-1,j]=254)and (map_[i-1,j]=false) then begin map_[i-1,j]:=true;map[i-1,j]:=Ni+1;end;
     if map[i,j+1]=253 then can:=true;
     if (map[i,j+1]=254)and (map_[i,j+1]=false) then begin map_[i,j+1]:=true;map[i,j+1]:=Ni+1;end;
     if map[i,j-1]=253 then can:=true;
     if (map[i,j-1]=254)and (map_[i,j-1]=false) then begin map_[i,j-1]:=true;map[i,j-1]:=Ni+1;end;
     ni:=ni+1;
     if can then puti;
    end; 
    Ni:=0; }
 repeat
 for i:=1 to 40 do
  for j:=1 to 40 do
   if map[i,j]=Ni then
    begin
     can:=false;
     if map[i+1,j]=253 then can:=true;
     if map[i+1,j]=254 then map[i+1,j]:=Ni+1;
     if map[i-1,j]=253 then can:=true;
     if map[i-1,j]=254 then map[i-1,j]:=Ni+1;
     if map[i,j+1]=253 then can:=true;
     if map[i,j+1]=254 then map[i,j+1]:=Ni+1;
     if map[i,j-1]=253 then can:=true;
     if map[i,j-1]=254 then map[i,j-1]:=Ni+1;
     //ni:=ni+1;
     if can then puti;
    end;
 Ni:=Ni+1;
 until Ni>nk;
 if Ni>Nk+1 then begin showmessage('BOLT !'); exit;end;
 puti;

end;

procedure tform1.Puti;
{var CXZ:array [1..8]of SKletka;
    c:integer;
    b,a:SKletka; }
begin
 form1.Grid.Canvas.Brush.Color:=cllime;
 form1.Grid.Canvas.Pen.Color:=clyellow;
 x:=x0;
 y:=y0;
 repeat
  if (map[x+1,y]<=map[x-1,y]) then
   begin
    if (map[x,y+1]<=map[x,y-1]) then
     begin
      if (map[x+1,y]<=map[x,y+1]) then
       begin
       //if (x+1=x2)and(y=y2) then begin x1:=x+2; y1:=y; end else
        x1:=x+1;
        y1:=y;

       end else
        begin
         x1:=x;y1:=y+1;
        end;
     end else
      begin
       if (map[x+1,y]<=map[x,y-1]) then
       begin
        x1:=x+1;
        y1:=y;
       end else
        begin
         x1:=x;
         y1:=y-1;
        end;
      end;
    end else
     begin
      if (map[x,y+1]<=map[x,y-1]) then
     begin
      if (map[x-1,y]<map[x,y+1]) then
       begin
        x1:=x-1;
        y1:=y;
       end else
        begin
         x1:=x;y1:=y+1;
        end;
     end else
      begin
       if (map[x-1,y]<map[x,y-1]) then
       begin
        x1:=x-1;
        y1:=y;
       end else
        begin
         x1:=x;
         y1:=y-1;
        end;
      end;
     end;
    ///
  if (map[x+1,y+1]<=map[x-1,y-1]) then
   begin
    if (map[x-1,y+1]<=map[x+1,y-1]) then
     begin
      if (map[x+1,y+1]<=map[x-1,y+1]) then
       begin
       //if (x+1=x2)and(y=y2) then begin x1:=x+2; y1:=y; end else
        x2:=x+1;
        y2:=y+1;

       end else
        begin
         x2:=x-1;y2:=y+1;
        end;
     end else
      begin
       if (map[x+1,y+1]<=map[x+1,y-1]) then
       begin
        x2:=x+1;
        y2:=y+1;
       end else
        begin
         x2:=x+1;
         y2:=y-1;
        end;
      end;
    end else
     begin
      if (map[x-1,y+1]<=map[x+1,y-1]) then
     begin
      if (map[x-1,y-1]<map[x-1,y+1]) then
       begin
        x2:=x-1;
        y2:=y-1;
       end else
        begin
         x2:=x-1;y2:=y+1;
        end;
     end else
      begin
       if (map[x-1,y-1]<map[x+1,y-1]) then
       begin
        x2:=x-1;
        y2:=y-1;
       end else
        begin
         x2:=x+1;
         y2:=y-1;
        end;
      end;
     end;
  ////
  if map[x1,y1]>=map[x2,y2] then
  begin
   x1:=x2;
   y1:=y2;
  end;
  form1.Grid.Canvas.Rectangle(x1*15+x1+2,y1*10+y1+2,x1*15+8+x1,y1*10+8+y1);
  x:=x1;
  y:=y1;
 until map[x1,y1]=0;
end;

procedure TForm1.GridClick(Sender: TObject);
begin
 map[grid.Selection.Left,grid.Selection.top]:=255;
 grid.Canvas.Brush.Color:=clred;
 grid.Canvas.Pen.Color:=clblue;
 for i:=1 to 40 do
  for j:=1 to 40 do
   if map[i,j]=255 then
    begin
     grid.Canvas.Rectangle(i*15+i+2,j*10+j+2,i*15+8+i,j*10+8+j);
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 for i:=1 to 40 do
  for j:=1 to 40 do
  begin
   map[i,j]:=254;
  end;

  for i:=1 to 40 do
   begin
    map[1,i]:=255;
    map[i,1]:=255;
    map[40,i]:=255;
    map[i,40]:=255;
   end;
 {for i:=1 to 40 do
  for j:=1 to 40 do
  begin
   map_[i,j]:=false;
  end;  }
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 grid.Refresh;
 FormCreate(sender);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
for i:=1 to 40 do
 for j:=1 to 40 do
 grid1.Cells[i,j]:=floattostr(map[i,j]);
end;

end.
