Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1.  
  2. import java.util.*;
  3. import java.awt.Point;
  4.  
  5. public class Labyrintti {
  6.  
  7. public ArrayDeque<Solmu> jono = new ArrayDeque<>();
  8. public ArrayList<Solmu> vieruslista = new ArrayList<>();
  9. public HashMap<String, Boolean> ovet = new HashMap<>();
  10. ArrayList<Character> doors;
  11. ArrayList<String> collectedKeys;
  12. public boolean[][] vierailtu;
  13. public int[][] etaisyys;
  14. public Solmu[][] edeltaja;
  15. public Point alku;
  16.  
  17. public String etsi(char[][] laby) {
  18. vierailtu = new boolean[laby.length][laby[0].length];
  19.  
  20. etaisyys = new int[laby.length][laby[0].length];
  21. for (int x = 0; x < vierailtu.length; x++) {
  22. for (int y = 0; y < vierailtu[x].length; y++) {
  23. vierailtu[x][y] = false;
  24. etaisyys[x][y] = Integer.MAX_VALUE;
  25. }
  26. }
  27. doors = new ArrayList<>();
  28. doors.add('A');
  29. doors.add('B');
  30. doors.add('C');
  31. doors.add('D');
  32. ArrayList<Character> keys = new ArrayList<>();
  33. keys.add('a');
  34. keys.add('b');
  35. keys.add('c');
  36. keys.add('d');
  37. collectedKeys = new ArrayList<>();
  38. edeltaja = new Solmu[laby.length][laby[0].length];
  39. ArrayList<Point> reitti = new ArrayList<>();
  40. Solmu alku = new Solmu(findPoint(laby, 'x'), collectedKeys, reitti);
  41.  
  42. jono.addLast(alku);
  43. vierailtu[alku.getCoordinates().y][alku.getCoordinates().x] = true;
  44. etaisyys[alku.getCoordinates().y][alku.getCoordinates().x] = 0;
  45. boolean yFound = false;
  46. while (!jono.isEmpty() && !yFound) {
  47. vieruslista.clear();
  48. Solmu solmu = jono.pop();
  49. addNeighbours(laby, solmu);
  50. for (Solmu naapuri : vieruslista) {
  51. char curNeighbor = laby[naapuri.getCoordinates().y][naapuri.getCoordinates().x];
  52. yFound = (curNeighbor == 'y');
  53. if (yFound) {
  54. edeltaja[naapuri.getCoordinates().y][naapuri.getCoordinates().x] = solmu;
  55. vierailtu[naapuri.getCoordinates().y][naapuri.getCoordinates().x] = true;
  56. break;
  57. }
  58. if (keys.contains(laby[naapuri.getCoordinates().y][naapuri.getCoordinates().x]) && !collectedKeys.contains(laby[naapuri.getCoordinates().y][naapuri.getCoordinates().x])) {
  59. String key = String.valueOf(laby[naapuri.getCoordinates().y][naapuri.getCoordinates().x]);
  60. collectedKeys.add(key);
  61. for (int x = 0; x < vierailtu.length; x++) {
  62. for (int y = 0; y < vierailtu[x].length; y++) {
  63. if (vierailtu[x][y]) {
  64. vierailtu[x][y] = false;
  65. }
  66. }
  67. }
  68. }
  69. vierailtu[naapuri.getCoordinates().y][naapuri.getCoordinates().x] = true;
  70. jono.addLast(naapuri);
  71. etaisyys[naapuri.getCoordinates().y][naapuri.getCoordinates().x] = etaisyys[solmu.getCoordinates().y][solmu.getCoordinates().x] + 1;
  72. edeltaja[naapuri.getCoordinates().y][naapuri.getCoordinates().x] = solmu;
  73. }
  74. }
  75. Solmu loppu = new Solmu(findPoint(laby, 'y'), collectedKeys, reitti);
  76. if (!vierailtu[loppu.getCoordinates().y][loppu.getCoordinates().x]) {
  77. return null;
  78. }
  79. String polku = "";
  80. System.out.println(loppu.getReitti().size());
  81. System.out.println(loppu.getReitti());
  82. ArrayList<Point> s = loppu.getReitti();
  83. for (int i = 1; i < s.size(); i++) {
  84. Point currCoord = s.get(i);
  85. Point prevCoord = s.get(i - 1);
  86. if (prevCoord.x == currCoord.x + 1) {
  87. polku = polku + "O";
  88. } else if (prevCoord.x == currCoord.x - 1) {
  89. polku = polku + "V";
  90. } else if (prevCoord.y == currCoord.y - 1) {
  91. polku = polku + "A";
  92. } else if (prevCoord.y == currCoord.y + 1) {
  93. polku = polku + "Y";
  94. }
  95. }
  96. return polku;
  97. }
  98.  
  99. public void addNeighbours(char[][] array, Solmu solmu) {
  100. Character m = array[solmu.getCoordinates().y - 1][solmu.getCoordinates().x];
  101. if (m != '#') {
  102. if (((doors.contains(m) && collectedKeys.contains(String.valueOf(m).toLowerCase())) || !doors.contains(m)) && !vierailtu[solmu.getCoordinates().y - 1][solmu.getCoordinates().x]) {
  103. Point point = new Point(solmu.getCoordinates().x, solmu.getCoordinates().y - 1);
  104. vieruslista.add(new Solmu(point, collectedKeys, solmu.getReitti()));
  105. }
  106. }
  107. //m = array[solmu.getCoordinates().x + 1][solmu.getCoordinates().y];
  108. m = array[solmu.getCoordinates().y + 1][solmu.getCoordinates().x];
  109. if (m != '#') {
  110. if (((doors.contains(m) && collectedKeys.contains(String.valueOf(m).toLowerCase())) || !doors.contains(m)) && !vierailtu[solmu.getCoordinates().y + 1][solmu.getCoordinates().x]) {
  111. Point point = new Point(solmu.getCoordinates().x, solmu.getCoordinates().y + 1);
  112. vieruslista.add(new Solmu(point, collectedKeys, solmu.getReitti()));
  113. }
  114. }
  115. //m = array[solmu.getCoordinates().x][solmu.getCoordinates().y - 1];
  116. m = array[solmu.getCoordinates().y][solmu.getCoordinates().x + 1];
  117. if (m != '#') {
  118. if (((doors.contains(m) && collectedKeys.contains(String.valueOf(m).toLowerCase())) || !doors.contains(m)) && !vierailtu[solmu.getCoordinates().y][solmu.getCoordinates().x + 1]) {
  119. Point point = new Point(solmu.getCoordinates().x + 1, solmu.getCoordinates().y);
  120. vieruslista.add(new Solmu(point, collectedKeys, solmu.getReitti()));
  121. }
  122. }
  123. //m = array[solmu.getCoordinates().x][solmu.getCoordinates().y + 1];
  124. m = array[solmu.getCoordinates().y][solmu.getCoordinates().x - 1];
  125. if (m != '#') {
  126. if (((doors.contains(m) && collectedKeys.contains(String.valueOf(m).toLowerCase())) || !doors.contains(m)) && !vierailtu[solmu.getCoordinates().y][solmu.getCoordinates().x - 1]) {
  127. Point point = new Point(solmu.getCoordinates().x - 1, solmu.getCoordinates().y);
  128. vieruslista.add(new Solmu(point, collectedKeys, solmu.getReitti()));
  129. }
  130. }
  131. }
  132.  
  133. public Point findPoint(char[][] array, char x) {
  134. Point location = new Point(0, 0);
  135. for (int row = 0; row < array.length; row++) {
  136. for (int col = 0; col < array[0].length; col++) {
  137. if (array[row][col] == x) {
  138. location.y = row;
  139. location.x = col;
  140. break;
  141. }
  142. }
  143. }
  144. return location;
  145. }
  146.  
  147. }
  148. _____________________________________________________________________________________________________________________________
  149.  
  150.  
  151. import java.awt.Point;
  152. import java.util.ArrayList;
  153.  
  154.  
  155. public class Solmu {
  156.  
  157. private Point koordinaatit;
  158. private ArrayList<String> avaimet;
  159. private ArrayList<Point> reitti = new ArrayList<>();
  160.  
  161. public Solmu(Point koordinaatit, ArrayList<String> avaimet, ArrayList<Point> reitti){
  162. this.koordinaatit = koordinaatit;
  163. this.avaimet = avaimet;
  164. this.reitti = reitti;
  165. this.koordinaatit = koordinaatit;
  166. this.reitti.add(this.koordinaatit);
  167. }
  168.  
  169. public ArrayList<Point> getReitti(){
  170. return reitti;
  171. }
  172.  
  173. public Point getCoordinates(){
  174. return this.koordinaatit;
  175. }
  176. }
  177. _________________________________________________________________________________________________________________________________
  178.  
  179.  
  180. import java.util.*;
  181.  
  182. public class Main {
  183.  
  184. public static void main(String[] args) {
  185. Labyrintti l = new Labyrintti();
  186. char[][] t = {{'#', '#', '#', '#', '#', '#', '#'},
  187. {'#', 'x', '#', '.', 'y', 'b', '#'},
  188. {'#', '.', '#', 'A', '#', '#', '#'},
  189. {'#', 'b', 'B', '.', 'B', 'a', '#'},
  190. {'#', '#', '#', '#', '#', '#', '#'}};
  191. System.out.println(l.etsi(t)); // AAOOOOVVYYO
  192.  
  193. // Labyrintti k = new Labyrintti();
  194. //// char[][] y = {{'#','#','#','#','#','#','#'},
  195. //// {'#','x','#','B','y','b','#'},
  196. //// {'#','.','#','A','#','#','#'},
  197. //// {'#','b','.','.','.','a','#'},
  198. //// {'#','#','#','#','#','#','#'}};
  199. // System.out.println(k.etsi(y)); // AAOOOOVVYYO
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement