program Pathfinder_load;

//This function simulates a ki algo to find the maze solution, locs=92

type 
  patharray=array[1..4] of integer;

type patharray2 = array[1..4]of patharray;
const MAXPATH = 4;

var path: patharray2;

//var path2: patharray2 = [((0,0,3,0),(5,6,7,0),(9,0,0,0),(13,14,0,0));
 i,goal, start:integer;

procedure initmaze;
begin
 path[1][1]:= 0;
 path[1][2]:= 0;
 path[1][3]:= 3;
 path[1][4]:= 0;
 path[2][1]:= 5;
 path[2][2]:= 6;
 path[2][3]:= 7;
 path[2][4]:= 0;
 path[3][1]:= 9;
 path[3][2]:= 0;
 path[3][3]:= 0;
 path[3][4]:= 0;
 path[4][1]:= 13;
 path[4][2]:= 14;
 path[4][3]:= 0;
 path[4][4]:= 0;
// ((0,0,3,0),(5,6,7,0),(9,0,0,0),(13,14,0,0));
end;
  

procedure MazeSearch(start,goal: integer);
var found,foun: boolean;
    j,z,y,a: integer;
begin
  found:= false;
  foun:= false;
  i:= 0;
  i:= i+1;
  j:= 0;
  repeat
  if j=4 then begin
    j:= 0;
    i:= i+1;
  end;
  j:= j+1;
  if start = path[i][j] then begin
      found:= true;
      a:=0;
      repeat
        a:= a+1;
        if a=1 then begin
            z:= i+1;
            y:= j;
            end;
        if a=2 then begin
            y:= j-1;
            z:= i;
            end;
        if a=3 then y:= j+1;
        if path[z][y]<>0 then begin
        foun:=true;
        writeln('go from'+inttostr(path[i][j])+' to '+inttostr(path[z][y]));
        end;
      until(foun);
    end;
  until(found)or(i=maxpath);
  if found then begin
    if path[z][y]<>goal then
      MazeSearch(path[z][y],goal)
    else
    writeln('key turn solution');
  end else
    writeln('no way');
end;

//main maze
begin
  i:= 0;
  writeln('start');
  start:= 1;
  writeln('goal');
  goal:= 4;
  initMaze;
  MazeSearch(start,goal);
end.  
