Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. package aed.caminos;
  2.  
  3. import es.upm.aedlib.Entry;
  4. import es.upm.aedlib.positionlist.PositionList;
  5. import es.upm.aedlib.positionlist.NodePositionList;
  6. import es.upm.aedlib.set.Set;
  7. import es.upm.aedlib.set.PositionListSet;
  8. import es.upm.aedlib.priorityqueue.PriorityQueue;
  9. import es.upm.aedlib.priorityqueue.HeapPriorityQueue;
  10. import es.upm.aedlib.map.Map;
  11. import es.upm.aedlib.map.HashTableMap;
  12. import es.upm.aedlib.graph.Edge;
  13. import es.upm.aedlib.graph.Vertex;
  14. import es.upm.aedlib.graph.DirectedGraph;
  15. import es.upm.aedlib.graph.DirectedAdjacencyListGraph;
  16.  
  17.  
  18. public class Transportistas {
  19. private DirectedGraph<Point,Integer> graph;
  20. private Map<Point,Vertex<Point>> points2vertices = new HashTableMap<Point,Vertex<Point>>();
  21.  
  22. private int coste(Point a, Point b, Integer[][] map) {
  23. return Math.max(map[b.getX()][b.getY()] - map[a.getX()][a.getY()], 0) + 1;
  24. }
  25.  
  26. public Transportistas(Integer[][] map) {
  27. graph = new DirectedAdjacencyListGraph<Point,Integer>();
  28. for(int i = 0; i< map[0].length; i++) {
  29. for(int j = 0; j< map.length; j++) {
  30. if(map[j][i] != null) {
  31. Point temp = new Point(j,i);
  32. points2vertices.put(temp, graph.insertVertex(temp));
  33.  
  34. }
  35. }
  36. }
  37. for(Vertex<Point> p : graph.vertices()) {
  38. int x = p.element().getX();
  39. int y = p.element().getY();
  40. for(Vertex<Point> v : graph.vertices()) {
  41. int otherX = v.element().getX();
  42. int otherY = v.element().getY();
  43. if(otherX == (x+1) && otherY == y){
  44. graph.insertDirectedEdge(p, v, coste(p.element(), v.element(),map));
  45. }
  46. if(otherX == (x-1) && otherY == y){
  47. graph.insertDirectedEdge(p, v, coste(p.element(), v.element(),map));
  48. }
  49. if(otherX == (x) && otherY == (y+1)){
  50. graph.insertDirectedEdge(p, v, coste(p.element(), v.element(),map));
  51. }
  52. if(otherX == (x) && otherY == (y-1)){
  53. graph.insertDirectedEdge(p, v, coste(p.element(), v.element(),map));
  54. }
  55.  
  56.  
  57.  
  58. }
  59.  
  60. }
  61.  
  62. }
  63.  
  64. public PositionList<Point> pathFromTo(Point fromPoint, Point toPoint) {
  65. PositionList<Point> res = new NodePositionList<Point>();
  66. Set<Edge<Integer>> lista = new PositionListSet<Edge<Integer>>();
  67. search(points2vertices.get(fromPoint), points2vertices.get(toPoint), lista);
  68. for(Edge<Integer> v : lista) {
  69. res.addLast(graph.endVertex(v).element());
  70. }
  71. res.addFirst(fromPoint);
  72.  
  73. if(res.isEmpty())
  74. res = null;
  75. return res;
  76. }
  77.  
  78. private Set<Edge<Integer>> search(Vertex<Point> inicial, Vertex<Point> ultimo ,Set<Edge<Integer>> lista){
  79. if(inicial==ultimo)
  80. return lista;
  81. if(lista.contains(graph.outgoingEdges(inicial)))
  82. return null;
  83. for(Edge<Integer> v : graph.outgoingEdges(inicial)) {
  84. lista.add(v);
  85. if(search(graph.endVertex(v),ultimo,lista) != null)
  86. return lista;
  87. else
  88. lista.remove(v);
  89. }
  90. return lista;
  91. }
  92.  
  93. public PositionList<Point> bestPathFromTo(Point fromPoint,Point toPoint) {
  94. return null;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement