import java.io.*;
public class Driver {
private File mapFile = new File("map.txt");
private char[][]maze;
private char pathMarker = 'a';
private int row,col,ndx=0;
public static void main(String[] args) {
Driver d = new Driver();
}
public Driver(){
loadMap(mapFile);
row=startRow(maze);
col=startColumn(maze);
maze[row][col]=' ';
findPath(maze,row,col);
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[i].length; j++) {
System.out.print(maze[i][j]);
}
System.out.println();
}
}
private void loadMap(File f){
try{
String line,line2;
int ndx=0,ndx2=0;
FileReader fReader,fReader2;
BufferedReader reader,reader2;
fReader = new FileReader(f);
reader = new BufferedReader(fReader);
while ((line = reader.readLine()) != null ) {
ndx++;
}
reader.close();
fReader2 = new FileReader(f);
reader2 = new BufferedReader(fReader2);
maze = new char [ndx][];
while ((line2 = reader2.readLine()) != null) {
maze[ndx2] = line2.toCharArray();
ndx2++;
}
reader2.close();
}catch(IOException e){
System.out.println("INVALID FILE");
}
}
public int startRow(char [][]arr){
int z=0;
for(int i=0;i!=arr.length;i++){
for(int j=0; j!=arr[i].length;j++){
if(arr[i][j]=='a'){
z=i;
}
}
}return z;
}
public int startColumn(char[][]arr){
int z=0;
for(int i=0;i!=arr.length;i++){
for(int j=0; j!=arr[i].length;j++){
if(arr[i][j]=='a'){
z=j;
}
}
}return z;
}
public boolean endFound(char[][] arr,int r, int c){
if ((!(r < arr.length)) || r < 0) {
return false;
}
if ((!(c < arr[r].length)) || c < 0) {
return false;
}
if ((r - 1) > -1) {
if (arr[r - 1][c] == '*') {
return true;
}
}
else if((r+1) <= arr.length){
if(arr[r+1][c] =='*'){
return true;
}
}
else if((c-1)>-1){
if(arr[r][c-1]=='*'){
return true;
}
}
else if((c+1)<=arr[r].length){
if(arr[r][c+1]=='*'){
return true;
}
}
return false;
}
public boolean findPath(char[][] arr, int r, int c){
if(r<0 || r>arr.length || c<0 || c>arr.length){
return false;
}
else if(endFound(arr,r,c)==true){
//else if(maze[r][c]=='*'){
System.out.println("FINISHED!");
for(int i=0;i!=arr.length;i++){
System.out.println(arr[i]);
}
return true;
}
else if(maze[r][c]=='#'){
return false;
}
else if(maze[r][c]!=' '){
return false;
}
else{
maze[r][c] = pathMarker;
pathMarker++;
ndx++;
if(ndx==26){
pathMarker='a';
ndx=0;
}
if(findPath(arr,r+1,c)){
return true;
}else if(findPath(arr,r,c+1)){
return true;
}else if(findPath(arr,r,c-1)){
return true;
}else if(findPath(arr,r-1,c)){
return true;
}else{
//maze[r][c]=' ';
return false;
}
}
}
}