Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <map>
  2. #include <queue>
  3. #include <set>
  4. struct vector2D {
  5. int x, y;
  6. bool operator < (vector2D b) const
  7. {
  8. return std::make_pair(x, y) > std::make_pair(b.x, b.y);
  9. }
  10. bool operator != (vector2D b) const
  11. {
  12. return (x != b.x || y != b.y);
  13. }
  14. bool operator == (vector2D b) const
  15. {
  16. return (x == b.x && y == b.y);
  17. }
  18. };
  19. template <class t>
  20. double pythagorean(t a, t b) {
  21. return sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y, 2));
  22. };
  23.  
  24. class node {
  25. public:
  26. double f;
  27. double g;
  28. vector2D pos;
  29. bool isClosed;
  30. bool operator < (node b) const {
  31. return b.f < f;
  32. };
  33. bool operator == (node b) const {
  34. pos == b.pos;
  35. }
  36. node() { g = INT_MAX; isClosed = false; };
  37. };
  38. std::set<vector2D> neighbors(vector2D currentPos)
  39. {
  40.  
  41. std::set<vector2D> toReturn;
  42. toReturn.insert({ currentPos.x - 1, currentPos.y - 1 });
  43. toReturn.insert({ currentPos.x - 0, currentPos.y - 1 });
  44. toReturn.insert({ currentPos.x + 1, currentPos.y - 1 });
  45. toReturn.insert({ currentPos.x - 1, currentPos.y - 0 });
  46. toReturn.insert({ currentPos.x + 1, currentPos.y - 0 });
  47. toReturn.insert({ currentPos.x - 1, currentPos.y + 1 });
  48. toReturn.insert({ currentPos.x - 0, currentPos.y + 1 });
  49. toReturn.insert({ currentPos.x + 1, currentPos.y + 1 });
  50.  
  51. return toReturn;
  52. };
  53. int main() {
  54. vector2D start = { 0,0 };
  55. vector2D finish = { 0,40};
  56.  
  57. std::priority_queue<node> openSet;
  58. std::map<vector2D, node*> createdNodes;
  59. node startNode;
  60. startNode.f = pythagorean(start, finish);
  61. startNode.g = 0;
  62. startNode.pos = start;
  63. createdNodes[start] = &startNode;
  64. openSet.push(startNode);
  65. while (!openSet.empty())
  66. {
  67. node currentNode = openSet.top();
  68. vector2D currentPos = currentNode.pos;
  69. if (currentPos == finish) {
  70. printf("Found Path!\n");
  71. break;
  72. }
  73. openSet.pop();
  74. createdNodes[currentPos]->isClosed = true;
  75.  
  76.  
  77. for (auto n : neighbors(currentPos)) {
  78.  
  79. node* neighbor = nullptr;
  80.  
  81. switch (createdNodes.count(n)) {
  82. case 0:
  83. neighbor = &node();
  84. createdNodes[n] = neighbor;
  85.  
  86. neighbor->pos = n;
  87. break;
  88. case 1:
  89. neighbor = createdNodes[n];
  90. break;
  91. }
  92. printf("current node is at %i, %i\n", neighbor->pos.x, neighbor->pos.y);
  93. if (neighbor->isClosed) {
  94. continue;
  95. }
  96.  
  97. double tenativeGScore = currentNode.g + pythagorean(currentPos, neighbor->pos);
  98.  
  99. neighbor->g = tenativeGScore;
  100. neighbor->f = pythagorean(neighbor->pos, finish);
  101. openSet.push(*neighbor);
  102. }
  103. }
  104. while(true){}
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement