Advertisement
Fef

Untitled

Fef
Nov 23rd, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. bool sortByX(const loc loc1, const loc loc2)
  2. {
  3.   return (((loc1.x*100)+loc1.y)<((loc2.x*100)+loc2.y));
  4. }
  5.  
  6. //Above is the alogorithm for sort and next_permutation
  7.  
  8. int findlength(vector<loc> dm, loc robot)//finds total length of a path and returns as an int
  9. {
  10.   int length=0;
  11.   length = ((abs(robot.x-dm[0].x))+(abs(robot.y-dm[0].y)));
  12.   for (int x=0; x<10; x++)//could be x<dm.size() for scaleabilty, but that seems outside the scope of project
  13.   {
  14.     length = length + ((abs(dm[x].x-dm[x+1].x))+(abs(dm[x].y-dm[x+1].y)));
  15.   }
  16.  
  17.   return length;
  18. }
  19.  
  20. void determineRoute(vector <loc> dm, loc robot)
  21. {
  22.   int shortestlength = findlength(dm,robot);
  23.   int curlength;
  24.   cout << shortestlength << endl;
  25.   for (int x=0; x<=3628800; x++) // <= value is 10!, since that's the number of paths for 10 diamonds, could be dm.size()!
  26. //WARNING: Doing dm.size()! would quickly take up too much memeory for non-trivial sizes of dm, because TSP is NP-hard
  27.   {
  28.     next_permutation(dm.begin(),dm.end(),sortByX);
  29.     curlength = findlength(dm,robot);
  30.    
  31.     if (curlength<shortestlength)
  32.     {
  33.       shortestlength = curlength;
  34.       TSP = dm; // Yay Vectors!
  35.     }
  36.   }
  37. }
  38.  
  39. action choose_next_step(int w, int h, loc robot, vector<loc> dm, ostream &log)
  40. {
  41.   if (firstrun)
  42.     {
  43.       sort(dm.begin(),dm.end(),sortByX);
  44.       determineRoute(dm, robot);
  45.       firstrun=false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement