@Test public void testReturnPathToPrincess() throws Exception { String[] result = BotSavesPrincess.returnPathToPrincess(new String[] {"---","-m-","p--"}); assertArrayEquals(new String[]{"DOWN", "LEFT"}, result); }
public static String[] returnPathToPrincess(String[] grid) { Pt princess = locOf('p', grid); Pt start = locOf('m', grid); List<String> verts = new ArrayList<>(); List<String> horz = new ArrayList<>(); if (start.row > princess.row) { for (int i = princess.row; i < start.row; i++) { verts.add("UP"); } } else if (start.row < princess.row) { for (int i = start.row; i < princess.row; i++) { verts.add("DOWN"); } } if (start.col > princess.col) { for (int i = princess.col; i < start.col; i++) { horz.add("LEFT"); } } else if (start.col < princess.col) { for (int i = start.col; i < princess.col; i++) { horz.add("RIGHT"); } } verts.addAll(horz); return verts.toArray(new String[verts.size()]); }
BotSavesPrincess { public static String[] returnPathToPrincess(String[] grid) { Pt princess = locOf('p', grid); Pt start = locOf('m', grid); List<String> verts = new ArrayList<>(); List<String> horz = new ArrayList<>(); if (start.row > princess.row) { for (int i = princess.row; i < start.row; i++) { verts.add("UP"); } } else if (start.row < princess.row) { for (int i = start.row; i < princess.row; i++) { verts.add("DOWN"); } } if (start.col > princess.col) { for (int i = princess.col; i < start.col; i++) { horz.add("LEFT"); } } else if (start.col < princess.col) { for (int i = start.col; i < princess.col; i++) { horz.add("RIGHT"); } } verts.addAll(horz); return verts.toArray(new String[verts.size()]); } }
BotSavesPrincess { public static String[] returnPathToPrincess(String[] grid) { Pt princess = locOf('p', grid); Pt start = locOf('m', grid); List<String> verts = new ArrayList<>(); List<String> horz = new ArrayList<>(); if (start.row > princess.row) { for (int i = princess.row; i < start.row; i++) { verts.add("UP"); } } else if (start.row < princess.row) { for (int i = start.row; i < princess.row; i++) { verts.add("DOWN"); } } if (start.col > princess.col) { for (int i = princess.col; i < start.col; i++) { horz.add("LEFT"); } } else if (start.col < princess.col) { for (int i = start.col; i < princess.col; i++) { horz.add("RIGHT"); } } verts.addAll(horz); return verts.toArray(new String[verts.size()]); }  }
BotSavesPrincess { public static String[] returnPathToPrincess(String[] grid) { Pt princess = locOf('p', grid); Pt start = locOf('m', grid); List<String> verts = new ArrayList<>(); List<String> horz = new ArrayList<>(); if (start.row > princess.row) { for (int i = princess.row; i < start.row; i++) { verts.add("UP"); } } else if (start.row < princess.row) { for (int i = start.row; i < princess.row; i++) { verts.add("DOWN"); } } if (start.col > princess.col) { for (int i = princess.col; i < start.col; i++) { horz.add("LEFT"); } } else if (start.col < princess.col) { for (int i = start.col; i < princess.col; i++) { horz.add("RIGHT"); } } verts.addAll(horz); return verts.toArray(new String[verts.size()]); }  static void main(String[] args); static String[] returnPathToPrincess(String[] grid); }
BotSavesPrincess { public static String[] returnPathToPrincess(String[] grid) { Pt princess = locOf('p', grid); Pt start = locOf('m', grid); List<String> verts = new ArrayList<>(); List<String> horz = new ArrayList<>(); if (start.row > princess.row) { for (int i = princess.row; i < start.row; i++) { verts.add("UP"); } } else if (start.row < princess.row) { for (int i = start.row; i < princess.row; i++) { verts.add("DOWN"); } } if (start.col > princess.col) { for (int i = princess.col; i < start.col; i++) { horz.add("LEFT"); } } else if (start.col < princess.col) { for (int i = start.col; i < princess.col; i++) { horz.add("RIGHT"); } } verts.addAll(horz); return verts.toArray(new String[verts.size()]); }  static void main(String[] args); static String[] returnPathToPrincess(String[] grid);  }