Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.GridLayout;
  3. import java.util.LinkedList;
  4.  
  5. import javax.swing.*;
  6.  
  7. public class AStern {
  8.  
  9.  
  10. //Definieren der Variablen
  11. public static LinkedList<Node> que = new LinkedList<Node>();
  12. public static LinkedList<Node> visited = new LinkedList<Node>();
  13.  
  14. public static int[][] arr_feld = new int[10][10];
  15.  
  16. public static int StartX = 1;
  17. public static int StartY = 1;
  18.  
  19. public static int TargetX = arr_feld[0].length-1;
  20. public static int TargetY = arr_feld[1].length-1;
  21.  
  22.  
  23. public static int QueCounter = 0;
  24. public static int GlobalCounter = 0;
  25. public static void getNeighbor(int x, int y) {
  26. /*
  27. * Abfragen der 4 Umliegenden felder
  28. * Y-1
  29. * X-1 X+1
  30. * Y+1
  31. */
  32. System.out.println(x + " " + y);
  33.  
  34.  
  35. if (y-1 >=0) {
  36. addQue(x, y-1);
  37. }
  38. if (y+1 <arr_feld[1].length) {
  39. addQue(x, y+1);
  40. }
  41.  
  42. if (x-1 >=0) {
  43. addQue(x-1, y);
  44. }
  45.  
  46. if (x+1 <arr_feld[0].length) {
  47. addQue(x+1, y);
  48. }
  49. }
  50.  
  51. public static void addQue(int x, int y) {
  52. //Erstellung des Objektes welches in que gespeichert wird
  53. Node n = new Node();
  54. n.setX(x);
  55. n.setY(y);
  56. n.setCost(calcCost(x,y));
  57. //Überprüfen ob Objekt schon gespeichert wurde
  58. QueCounter++;
  59. if (!que.contains(n) && !visited.contains(n)) {
  60. if (x-1 != TargetX && x-1 != TargetX && y != TargetY && y != TargetY) {
  61. //Objekt zu que hinzufügen
  62. que.add(n);
  63. //arr_feld[x][y] = 1;
  64. //Visuelle Darstellung welche zahl ausgewählt wurde
  65. if(QueCounter == 4) {
  66. QueCounter = 0;
  67.  
  68. getMinCost();
  69.  
  70. }
  71. } else {
  72. System.out.println("Ende" + x + "/" + y + "|" + TargetX + "/" + TargetY);
  73. }
  74. }
  75. }
  76.  
  77. public static void getMinCost() {
  78. int min = -1;
  79. int index;
  80. int current;
  81. int x = 0;
  82. int y = 0;
  83. //für jedes object in que
  84. for (int i = 0; i < que.size(); i++) {
  85. Node n = que.get(i);
  86.  
  87. //Zur initalisierung die erste zahl in min speichern
  88. current = n.getCost();
  89. if (min == -1) {
  90. min = n.getCost();
  91. index = i;
  92. x= n.getX();
  93. y = n.getY();
  94. }
  95.  
  96. //wenn die momentane Zahl kleiner als die gespeicherte ist, ersetzte sie.
  97. if (current < min) {
  98. index = i;
  99. min = n.getCost();
  100. x= n.getX();
  101. y = n.getY();
  102. }
  103. }
  104. output();
  105.  
  106. //Objekt von que löschen und in visited speichern
  107. int counter = que.size()-1;
  108. for (int i = 0; i < counter; i++) {
  109. visited.add(que.get(i));
  110. }
  111. que.clear();
  112.  
  113.  
  114. //Visuelle Darstellung welche zahl ausgewählt wurde
  115. arr_feld[x][y] = 2;
  116.  
  117. //Nachbar mit der Niedrigsten Zelle aufrufen
  118. getNeighbor(x, y);
  119.  
  120. }
  121.  
  122. public static void output() {
  123. for (int i = 0; i < arr_feld[0].length; i++) {
  124. for (int j = 0; j < arr_feld[1].length; j++) {
  125. System.out.print(arr_feld[i][j] + " \t");
  126. }
  127. System.out.println();
  128. }
  129. arr_feld[TargetX][TargetY] = 999;
  130. System.out.println("\n");
  131. }
  132.  
  133. public static int calcCost(int x, int y) {
  134. /*
  135. * Berechnen der kosten anhand der 'Manhatten Distance'
  136. * |Xstart - Xziel)| + |(Ystart - Yziel)|
  137. */
  138.  
  139. int cost = (int) Math.abs((x - TargetX) + (y - TargetY));
  140. return cost;
  141. }
  142.  
  143. public static void main(String[] args) {
  144. getNeighbor(StartX, StartY);
  145.  
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement