Advertisement
Fef

ugh im gay

Fef
Nov 23rd, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 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.  
  26.   for (int x=0; x<=3628800; x++) // <= value is 10!, since that's the number of paths for 10 diamonds, could be dm.size()!
  27. //WARNING: Doing dm.size()! would quickly take up too much memeory for non-trivial sizes of dm, because TSP is NP-hard
  28.   {
  29.     next_permutation(dm.begin(),dm.end(),sortByX);
  30.     curlength = findlength(dm,robot);
  31.  
  32.     if (curlength<shortestlength)
  33.     {
  34.       shortestlength = curlength;
  35.       TSP = dm; // Yay Vectors!
  36.     }
  37.   }
  38. }
  39.  
  40. action choose_next_step(int w, int h, loc robot, vector<loc> dm, ostream &log)
  41. {
  42.   if (firstrun)
  43.   {
  44.     sort(dm.begin(),dm.end(),sortByX);
  45.     determineRoute(dm, robot);
  46.     firstrun=false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement