import java.util.*; public class MaizeShortestPath { static Coord goal; static int minimal; static Coord ROOT = new Coord(0,0); public static void main(String[] args){ String input = "OXXO\n" + "OXOO\n" + "OOXO\n" + "OXOO\n"; String input2 = "OXXOO\n" + "OXOXO\n" + "OXOOO\n" + "OOXXO\n"; String[] arr = {input,input2}; for(String str : arr){ Scanner s = new Scanner( str); List lst = new ArrayList(); int cols = 0; while(s.hasNextLine()){ String line = s.nextLine(); char[] c = new char[ line.length()]; for(int i=0;i path = new ArrayList(); public static void start(char[][] iArr){ Coord root = new Coord(0,0); path.add(root); start(iArr,root); } static void prompt(){ Scanner s = new Scanner(System.in); s.nextLine(); } public static void start(char[][] iArr, Coord root){ //System.out.println("processing the root:" + root); //prompt(); List lst = findChildren(iArr,root); if(lst.size() <= 1 && !root.equals( ROOT )) { path.remove(path.size() -1); return; } // if the root has the goal as a direct child, then finish if(lst.indexOf(goal)!=-1){ minimal = Math.min(minimal, path.size()); System.out.println("New path:" + path); path.remove(path.size()-1); return; } int length = 1; for(int i=0;i map = new HashMap(); public static boolean isIn(List lst, Coord coord){ return lst.indexOf(coord) != -1; } public static List findChildren(char[][] iarr, Coord coord){ List lst = new ArrayList(); int x = coord.x; int y = coord.y; int rows = iarr.length; int cols = iarr[0].length; if(x+1 < rows && iarr[x+1][y] == 'O') lst.add(new Coord(x+1,y)); if(x-1 >=0 && iarr[x-1][y] == 'O') lst.add(new Coord(x-1,y)); if(y+1 < cols && iarr[x][y+1] == 'O') lst.add(new Coord(x,y+1)); if(y-1 >=0 && iarr[x][y-1] == 'O') lst.add(new Coord(x,y-1)); if(x+1< rows && y+1=0 && y-1>=0 && iarr[x-1][y-1]=='O') lst.add(new Coord(x-1,y-1)); if(x-1>=0 && y+1=0 && iarr[x+1][y-1]=='O') lst.add(new Coord(x+1,y-1)); return lst; } static void printArray(char[][] mat){ for(int i=0;i