Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * @author Max Fuerstenberg
- */
- public class Main {
- private static int totalRows;
- private static int totalColumns;
- private static List<List<Position>> successfullPaths = new ArrayList<>();
- public static void main(String[] args) {
- String input = "2\n" +
- "5 8 2\n" +
- "S....1..\n" +
- ".######.\n" +
- "..2.#...\n" +
- ".######.\n" +
- "......E.\n" +
- "1 11 2\n" +
- "S...1...E.2\n";
- Scanner reader = new Scanner(input);
- int n = reader.nextInt();
- for (int set = 0; set < n; set++) {
- totalRows = reader.nextInt();
- totalColumns = reader.nextInt();
- int d = reader.nextInt(); // checkpoints
- reader.nextLine();
- char[][] matrix = new char[totalRows][totalColumns];
- for (int j = 0; j < totalRows; j++) {
- matrix[j] = reader.nextLine().toCharArray();
- }
- // char[][] matrix = {{'S', '.', '#'}, {'#', '.', '.'}, {'#', '.', 'E'}};
- Position start = null;
- for (int row = 0; row < matrix.length; row++) {
- for (int column = 0; column < matrix[row].length; column++) {
- if (matrix[row][column] == 'S') {
- start = new Position(row, column);
- }
- }
- }
- // start = new Position(0, 1);
- System.out.println(start);
- for (char[] row : matrix) {
- System.out.println(Arrays.toString(row));
- }
- List<List<Position>> godPaths = new ArrayList<>();
- for (int i = 1; i <= d; i++) {
- successfullPaths = new ArrayList<>();
- search(matrix, start, Character.forDigit(i, 10), new ArrayList<>());
- int shortestIndex = -1;
- int smallest = Integer.MAX_VALUE;
- for (int j = 0; j < successfullPaths.size(); j++) {
- List<Position> path = successfullPaths.get(j);
- if (path.size() < smallest) {
- smallest = path.size();
- shortestIndex = j;
- }
- }
- godPaths.add(successfullPaths.get(shortestIndex));
- start = successfullPaths.get(shortestIndex).get(successfullPaths.get(shortestIndex).size() - 1);
- }
- successfullPaths = new ArrayList<>();
- search(matrix, start, 'E', new ArrayList<>());
- int shortestIndex = -1;
- int smallest = Integer.MAX_VALUE;
- for (int j = 0; j < successfullPaths.size(); j++) {
- List<Position> path = successfullPaths.get(j);
- if (path.size() < smallest) {
- smallest = path.size();
- shortestIndex = j;
- }
- }
- godPaths.add(successfullPaths.get(shortestIndex));
- int totalLength = 0;
- for (List<Position> path : godPaths) {
- totalLength += path.size();
- System.out.println(path);
- }
- System.out.println(totalLength);
- }
- }
- private static void search(char[][] matrix, Position start, char target, List<Position> oldMoves) {
- List<Position> moves = new ArrayList<>();
- moves.add(new Position(start.row - 1, start.column));
- moves.add(new Position(start.row + 1, start.column));
- moves.add(new Position(start.row, start.column - 1));
- moves.add(new Position(start.row, start.column + 1));
- for (Position move : moves) {
- List<Position> path = new ArrayList<>(oldMoves);
- try {
- char found = matrix[move.row][move.column];
- if (!path.contains(move)) {
- path.add(move);
- if (found == target) {
- successfullPaths.add(path);
- } else if (found != '#') {
- search(matrix, move, target, path);
- }
- }
- } catch (ArrayIndexOutOfBoundsException ignored) {}
- }
- }
- }
- class Position {
- int row;
- int column;
- public Position(int row, int column) {
- this.row = row;
- this.column = column;
- }
- @Override
- public String toString() {
- return "Position{" +
- "row=" + row +
- ", column=" + column +
- '}';
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Position position = (Position) o;
- return row == position.row &&
- column == position.column;
- }
- @Override
- public int hashCode() {
- return Objects.hash(row, column);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement