program TheMaze; type mazearray = array[1..50,1..50] of char; var infile,outfile:text; x, y, z: integer; R, C, shortest: integer; maze: mazearray; startrow,startcol,endrow,endcol: integer; procedure findshortest(row,col,numdots:integer;maze:mazearray); begin if numdots <= shortest then begin if (row=endrow) and (col=endcol) then begin if numdots - 1 < shortest then shortest := numdots - 1; end else begin maze[row,col] := '#'; if (maze[row-1,col] = '.') or (maze[row-1,col] = 'X') then findshortest(row-1,col,numdots+1,maze); if (maze[row+1,col] = '.') or (maze[row+1,col] = 'X') then findshortest(row+1,col,numdots+1,maze); if (maze[row,col-1] = '.') or (maze[row,col-1] = 'X') then findshortest(row,col-1,numdots+1,maze); if (maze[row,col+1] = '.') or (maze[row,col+1] = 'X') then findshortest(row,col+1,numdots+1,maze); end; end; end; begin assign(infile,'DATA21.txt'); reset(infile); assign(outfile,'OUT21.txt'); rewrite(outfile); for x:= 1 to 5 do begin readln(infile,R,C); shortest := R * C; for y:=1 to 50 do for z:= 1 to 50 do maze[y,z]:=' '; for y := 1 to R do begin for z := 1 to C do begin read(infile,maze[y,z]); if maze[y,z]='E' then begin startrow:=y; startcol:=z; end; if maze[y,z]='X' then begin endrow:=y; endcol:=z; end; end; readln(infile); end; if startrow = 1 then startrow:=2 else if startrow = R then startrow:=R-1; if startcol = 1 then startcol:=2 else if startcol = C then startcol:=C-1; findshortest(startrow,startcol,1,maze); writeln(outfile, shortest); end; close(infile); close(outfile); end.