Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /**
  4. * @author Max Fuerstenberg
  5. */
  6. public class Main {
  7.  
  8. private static int totalRows;
  9. private static int totalColumns;
  10. private static List<List<Position>> successfullPaths = new ArrayList<>();
  11.  
  12. public static void main(String[] args) {
  13. String input = "2\n" +
  14. "5 8 2\n" +
  15. "S....1..\n" +
  16. ".######.\n" +
  17. "..2.#...\n" +
  18. ".######.\n" +
  19. "......E.\n" +
  20. "1 11 2\n" +
  21. "S...1...E.2\n";
  22. Scanner reader = new Scanner(input);
  23. int n = reader.nextInt();
  24. for (int set = 0; set < n; set++) {
  25. totalRows = reader.nextInt();
  26. totalColumns = reader.nextInt();
  27. int d = reader.nextInt(); // checkpoints
  28. reader.nextLine();
  29. char[][] matrix = new char[totalRows][totalColumns];
  30. for (int j = 0; j < totalRows; j++) {
  31. matrix[j] = reader.nextLine().toCharArray();
  32. }
  33. // char[][] matrix = {{'S', '.', '#'}, {'#', '.', '.'}, {'#', '.', 'E'}};
  34.  
  35. Position start = null;
  36. for (int row = 0; row < matrix.length; row++) {
  37. for (int column = 0; column < matrix[row].length; column++) {
  38. if (matrix[row][column] == 'S') {
  39. start = new Position(row, column);
  40. }
  41. }
  42. }
  43.  
  44. // start = new Position(0, 1);
  45. System.out.println(start);
  46. for (char[] row : matrix) {
  47. System.out.println(Arrays.toString(row));
  48. }
  49. List<List<Position>> godPaths = new ArrayList<>();
  50. for (int i = 1; i <= d; i++) {
  51. successfullPaths = new ArrayList<>();
  52. search(matrix, start, Character.forDigit(i, 10), new ArrayList<>());
  53. int shortestIndex = -1;
  54. int smallest = Integer.MAX_VALUE;
  55. for (int j = 0; j < successfullPaths.size(); j++) {
  56. List<Position> path = successfullPaths.get(j);
  57. if (path.size() < smallest) {
  58. smallest = path.size();
  59. shortestIndex = j;
  60. }
  61. }
  62. godPaths.add(successfullPaths.get(shortestIndex));
  63. start = successfullPaths.get(shortestIndex).get(successfullPaths.get(shortestIndex).size() - 1);
  64. }
  65.  
  66. successfullPaths = new ArrayList<>();
  67. search(matrix, start, 'E', new ArrayList<>());
  68. int shortestIndex = -1;
  69. int smallest = Integer.MAX_VALUE;
  70. for (int j = 0; j < successfullPaths.size(); j++) {
  71. List<Position> path = successfullPaths.get(j);
  72. if (path.size() < smallest) {
  73. smallest = path.size();
  74. shortestIndex = j;
  75. }
  76. }
  77. godPaths.add(successfullPaths.get(shortestIndex));
  78.  
  79. int totalLength = 0;
  80. for (List<Position> path : godPaths) {
  81. totalLength += path.size();
  82. System.out.println(path);
  83. }
  84. System.out.println(totalLength);
  85. }
  86. }
  87.  
  88. private static void search(char[][] matrix, Position start, char target, List<Position> oldMoves) {
  89. List<Position> moves = new ArrayList<>();
  90. moves.add(new Position(start.row - 1, start.column));
  91. moves.add(new Position(start.row + 1, start.column));
  92. moves.add(new Position(start.row, start.column - 1));
  93. moves.add(new Position(start.row, start.column + 1));
  94.  
  95. for (Position move : moves) {
  96. List<Position> path = new ArrayList<>(oldMoves);
  97. try {
  98. char found = matrix[move.row][move.column];
  99. if (!path.contains(move)) {
  100. path.add(move);
  101. if (found == target) {
  102. successfullPaths.add(path);
  103. } else if (found != '#') {
  104. search(matrix, move, target, path);
  105. }
  106. }
  107. } catch (ArrayIndexOutOfBoundsException ignored) {}
  108. }
  109. }
  110. }
  111.  
  112. class Position {
  113. int row;
  114. int column;
  115.  
  116. public Position(int row, int column) {
  117. this.row = row;
  118. this.column = column;
  119. }
  120.  
  121. @Override
  122. public String toString() {
  123. return "Position{" +
  124. "row=" + row +
  125. ", column=" + column +
  126. '}';
  127. }
  128.  
  129. @Override
  130. public boolean equals(Object o) {
  131. if (this == o) return true;
  132. if (o == null || getClass() != o.getClass()) return false;
  133. Position position = (Position) o;
  134. return row == position.row &&
  135. column == position.column;
  136. }
  137.  
  138. @Override
  139. public int hashCode() {
  140. return Objects.hash(row, column);
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement