Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package ee.ttu.algoritmid.trampoline;
  2.  
  3. import java.util.*;
  4.  
  5. public class HW02 {
  6. private Queue<Node> path;
  7. private int[][] map;
  8. private Node finish;
  9.  
  10. public List<String> findMinJumps(int[][] mapOrigin) {
  11. this.map = deepCopy(mapOrigin); // V
  12. this.path = new LinkedList<>();
  13. this.finish = getFinish(); // 1
  14. path.add(finish); // 1
  15.  
  16. Node current;
  17. Node best = null;
  18.  
  19. while (!path.isEmpty()) { // sqrt V * V * log V
  20. current = path.poll(); // log V
  21. if (current.getX() == 0 && current.getY() == 0) { // 1
  22. best = current;
  23. break;
  24. }
  25. addPossiblePath(current); // sqrt(V) * log V
  26. }
  27. return composeAnswer(best);
  28. }
  29.  
  30. private void eliminateRowsWithOnes(int x, int y) {
  31. int standardJump = map[x][y];
  32. if (standardJump == 1) {
  33. int xLocal = x;
  34. boolean canReplace = false;
  35. outer:
  36. while (xLocal != -1) {
  37. for (int j = y - 1; j >= 0; j--) {
  38. if (map[xLocal][j] != standardJump) {
  39. break outer;
  40. }
  41. if (canReplace) {
  42. map[xLocal + 1][j] = -1;
  43. }
  44. }
  45. canReplace = true;
  46. xLocal--;
  47. }
  48. }
  49. }
  50.  
  51. private static int[][] deepCopy(int[][] original) {
  52. final int[][] result = new int[original.length][];
  53. for (int i = 0; i < original.length; i++) {
  54. result[i] = Arrays.copyOf(original[i], original[i].length);
  55. }
  56. return result;
  57. }
  58.  
  59. private Node getFinish() {
  60. return new Node(map.length - 1, map[0].length - 1, 0, null);
  61. }
  62.  
  63. private void addPossiblePath(Node target) { // sqrt(V) * log V
  64. for (int i = 0; i < target.getX(); i++) { // sqrt(V) * log V
  65. int y = target.getY(); // 1
  66. addToQueue(map[i][y], i, y, target); // log V
  67. }
  68.  
  69. for (int i = 0; i < target.getY(); i++) { // sqrt(V) * log V
  70. int x = target.getX();
  71. addToQueue(map[x][i], x, i, target); // log V
  72. }
  73. }
  74.  
  75. private void addToQueue(int jump, int x, int y, Node target) { // log V
  76. if (jump != -1) { //1
  77. Node possible = new Node(x, y, jump, target); // 1
  78. int shift = canReachPlusMinus(possible, target); // 1
  79. if (Math.abs(shift) <= 1) { // 1
  80. possible.setJump(jump + shift); // 1
  81. possible.postConstruct(); // 1
  82. path.add(possible); // log V
  83. if (!(x == 0 && y == 0)) { // 1
  84. map[x][y] = -1; // 1
  85. }
  86. }
  87. }
  88. }
  89.  
  90. private int canReachPlusMinus(Node jumpFrom, Node target) { // 1
  91. if (jumpFrom.getX() == target.getX()) { //1
  92. return target.getY() - (jumpFrom.getY() + jumpFrom.getJump());
  93. } else {
  94. return target.getX() - (jumpFrom.getX() + jumpFrom.getJump());
  95. }
  96. }
  97.  
  98. private List<String> composeAnswer(Node curNode) {
  99. List<String> result = new ArrayList<>();
  100. if (curNode != null) {
  101. while (curNode != finish) {
  102. result.add(curNode.toString());
  103. curNode = curNode.getPrevious();
  104. }
  105. }
  106. return result;
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement