tachia

Untitled

Jul 4th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 KB | None | 0 0
  1. /******************************************************************************
  2.  * File: Trailblazer.cpp
  3.  *
  4.  * Implementation of the graph algorithms that comprise the Trailblazer
  5.  * assignment.
  6.  */
  7. #include  "Trailblazer.h"
  8. #include "TrailblazerGraphics.h"
  9. #include "TrailblazerTypes.h"
  10. #include "TrailblazerPQueue.h"
  11.  
  12. using namespace std;
  13.  
  14. /* Function: shortestPath
  15.  *
  16.  * Finds the shortest path between the locations given by start and end in the
  17.  * specified world.  The cost of moving from one edge to the next is specified
  18.  * by the given cost function.  The resulting path is then returned as a
  19.  * Vector<Loc> containing the locations to visit in the order in which they
  20.  * would be visited.    If no path is found, this function should report an
  21.  * error.
  22.  *
  23.  * In Part Two of this assignment, you will need to add an additional parameter
  24.  * to this function that represents the heuristic to use while performing the
  25.  * search.  Make sure to update both this implementation prototype and the
  26.  * function prototype in Trailblazer.h.
  27.  */
  28. Vector<Loc>
  29. shortestPath(Loc start,
  30.              Loc end,
  31.              Grid<double>& world,
  32.              double costFn(Loc from, Loc to, Grid<double>& world),
  33.              double heuristic(Loc start, Loc end, Grid<double>& world)) {
  34.  
  35.             Map<Loc,Info> allInformation;
  36.             Vector<Loc> wayYouMetHer;
  37.              TrailblazerPQueue<Loc> discuss;          
  38.             makeList(allInformation,world);
  39.             Info inform=allInformation.get(start);
  40.             inform.color="YELLOW";
  41.             colorCell(world,start,YELLOW);
  42.             inform.way = 0;    
  43.             allInformation.put(start,inform);
  44.             discuss.enqueue(start,heuristic(start,end,world));
  45.             while(!discuss.isEmpty()){
  46.                 Loc loc=discuss.dequeueMin();
  47.                 Info inform=allInformation.get(loc);
  48.                 inform.color="GREEN";
  49.                 colorCell(world,loc,GREEN);
  50.                 allInformation.put(loc,inform);
  51.                 if(loc==end){
  52.                     break;
  53.                 }
  54.                 Set<Loc> neighbours=makeNeighbours(loc,world);
  55.                 double ownDistance=allInformation.get(loc).way;
  56.                 foreach(Loc loca in neighbours){
  57.                         Info inf=allInformation.get(loca);                  
  58.                         double newDistance=ownDistance+costFn(loc,loca,world);
  59.                         double newPr = newDistance + heuristic(loca,end,world);
  60.                         if(inf.color=="GRAY"){
  61.                             inf.color="YELLOW";
  62.                             colorCell(world,loca,YELLOW);
  63.                             inf.way=newDistance;
  64.                             inf.parent=loc;
  65.                             discuss.enqueue(loca,newPr);
  66.                             allInformation.put(loca,inf);
  67.                         }else if(inf.color=="YELLOW" && (inf.way>newDistance)){
  68.                             inf.way=newDistance;
  69.                             inf.parent=loc;
  70.                             discuss.decreaseKey(loca,newPr);
  71.                             allInformation.put(loca,inf);
  72.                        
  73.                         }
  74.                       }
  75.                 }
  76.     wayYouMetHer =  findWay(start,end,allInformation);      
  77.     return wayYouMetHer;
  78. }
  79.  
  80.  
  81. Vector <Loc> findWay(Loc & start,Loc& end,Map<Loc,Info>& allInformation) {
  82.     Vector <Loc> result;
  83.     Loc curr = end;
  84.     result.add(end);
  85.     while(true) {
  86.         curr = allInformation.get(curr).parent;
  87.         result.add(curr);
  88.         if(curr == start) {
  89.             break;
  90.         }
  91.     }
  92.     for(int i=0;i<result.size();i++) {
  93.         Loc remainder = result[i];
  94.         result[i] = result[result.size()-i-1];
  95.         result[result.size()-i-1] = remainder;
  96.     }
  97.     return result;
  98. }
  99.  
  100.  
  101. Set<Loc> makeNeighbours(Loc & loc, Grid<double> & world){
  102.     Set<Loc> neighbours;
  103.     Loc neighbour;
  104.     for(int i=-1; i<=1; i++){
  105.         for(int j=-1; j<=1; j++){
  106.             neighbour = makeLoc(loc.row+j,loc.col+i);
  107.             if(neighbour.col!=-1 && neighbour.col!=world.numCols() && neighbour.row                      
  108.                 !=-1 && neighbour.row!=world.numRows()){
  109.                     neighbours.add(neighbour);
  110.             }
  111.         }
  112.     }
  113.     return neighbours;
  114. }              
  115.      
  116.  
  117. void makeList ( Map<Loc,Info> &allInf, Grid<double> & world){
  118.     for(int i=0; i<world.numCols(); i++){
  119.         for(int j=0; j<world.numRows(); j++){
  120.             Loc loca = makeLoc(j,i);
  121.             Info inform;            
  122.             inform.color="GRAY";
  123.             inform.way=0;
  124.             allInf.put(loca,inform);
  125.  
  126.         }
  127.     }
  128.  
  129.  
  130. }
  131.  
  132. Set<Edge> createMaze(int numRows, int numCols) {
  133.     // TODO: Fill this in!
  134.     error("createMaze is not implemented yet.");
  135.     return Set<Edge>();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment