This is an implementation of a depth-search algorithm. It won't find the shortest path, but it marks the fields in an array until it arrives at the destination.

0 is the code for the destination field.
Also, this is by no means an optimal solution.

[pascal]
procedure TForm1.FindPath;
var
CurrentX, CurrentY: Integer;
CostN, CostE, CostS, CostW: Integer;
OldX, OldY: Integer;

function Smallest(A, B, C, D: Integer): Boolean;
begin
if (A <= B) and (A <= C) and (A <= D) then
Result := true
else
Result := false;
end;

begin
CurrentX := StartX;
CurrentY := StartY;

repeat
Maze[CurrentX, CurrentY] := Maze[CurrentX, CurrentY] + 1;

CostN := Maze[CurrentX, CurrentY - 1];
CostE := Maze[CurrentX + 1, CurrentY];
CostS := Maze[CurrentX, CurrentY + 1];
CostW := Maze[CurrentX - 1, CurrentY];

if Smallest(CostN, CostE, CostS, CostW) then
CurrentY := CurrentY - 1
else if Smallest(CostE, CostS, CostW, CostN) then
CurrentX := CurrentX + 1
else if Smallest(CostS, CostW, CostN, CostE) then
CurrentY := CurrentY + 1
else if Smallest(CostW, CostN, CostE, CostS) then
CurrentX := CurrentX - 1;
until Maze[CurrentX, CurrentY] = 0;
end;
[/pascal]