tachia

Untitled

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