graphmap.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #pragma once
  2. #pragma once
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <locale.h>
  6. #include <stdio.h>
  7. #include <string>
  8. #include "img.h"
  9. #include <math.h>
  10. #include <set>
  11. #include <queue>
  12. using namespace std;
  13. #pragma comment(linker, "/STACK:100000000")
  14. int direction[8][2] = { { -1,1 } ,{ 0,1 },{ 1,0 },{ 1,1 },{ 0,-1 },{ -1,0 },{ -1,-1 },{ 1,-1 }};
  15. class Position
  16. {
  17. public:
  18. int x, y;
  19. Position()
  20. {
  21. this->x = 0;
  22. this->y = 0;
  23. };
  24. Position(int x, int y)
  25. {
  26. this->x = x;
  27. this->y = y;
  28. }
  29. float calcDist(Position other) // ðàñ÷åò ðàññòîÿíèé ìåæäó ïîçèöèÿìè
  30. {
  31. x = this->x - other.x;
  32. y = this->y - other.y;
  33. return sqrt(x*x + y*y);
  34. }
  35. };
  36. class Node
  37. {
  38. public:
  39. bool vizited;
  40. int w; //âåñ óçëà (èëè åãî âîçâûøåííîñòü)
  41. int distation; // ðàññòîÿíèå äî íóæíîé òî÷êè (èëè âûõîäà)
  42. int dirCount; //êîëè÷åñòâî âîçìîæíûõ íàïðàâëåíèé
  43. Pixel data;
  44. Position pos;
  45. Node *branches[8];
  46. Node() {
  47. this->distation = 100000000000.0;
  48. this->dirCount = 0;
  49. this->w = 0;
  50. this->vizited = 0;
  51. pos = Position(0, 0);
  52. for (int i = 0; i < 8; i++)
  53. {
  54. branches[i] = (Node*)malloc(sizeof(Node));
  55. }
  56. };
  57. Node(Pixel data, int x, int y)
  58. {
  59. this->data = data;
  60. this->calcWeight();
  61. pos = Position(x, y);
  62. this->dirCount = 0;
  63. this->distation = 100000000000.0;
  64. this->vizited = 0;
  65. for (int i = 0; i < 8; i++)
  66. {
  67. branches[i] = (Node*)malloc(sizeof(Node));
  68. }
  69. }
  70. void calcWeight()
  71. {
  72. // c÷èòàåì âåñ óçëà ñ ïîìîùüþ êàêîé-ëèáî ôóíêöèè
  73. this->w = 255 - ((this->data.r + this->data.b + this->data.g) / 3) + 30;
  74. if (this->w > 200)
  75. this->w = 1000;
  76. }
  77. bool calcDistation(Node previos)
  78. {
  79. //ñ÷èòàåì ðàññòîÿíèå ïðîéäåííîãî ïóòè â äàííîé âåðøèíå
  80. float t = previos.pos.calcDist(this->pos);
  81. // cout << t <<endl;
  82. float dist = previos.distation + this->w + t;
  83. //cout << dist << endl;
  84. // åñëè ìåíüøå , ìåíÿåì òåêóùåå çíà÷åíèå ðàññòîÿíèÿ
  85. if (dist < this->distation)
  86. {
  87. this->distation = dist;
  88. return true;
  89. }
  90. return false;
  91. }
  92. void printPos()
  93. {
  94. cout << this->pos.x << " - ";
  95. cout << this->pos.y << endl;
  96. }
  97. bool operator < (const Node &v2) const
  98. {
  99. return this->distation < v2.distation;
  100. }
  101. bool operator > (const Node &v2) const
  102. {
  103. return this->distation > v2.distation;
  104. }
  105. };
  106. struct fo_sort
  107. {
  108. bool operator()(const Node *x, const Node *y) const
  109. {
  110. return *x > *y;
  111. }
  112. };
  113. class Graph : public Img <Node>
  114. {
  115. BMP map;
  116. Position start;
  117. public:
  118. /////////
  119. Graph(BMP map)
  120. {
  121. this->width = map.width;
  122. this->height = map.height;
  123. this->map = map;
  124. init();
  125. for (int x = 0; x < map.height; x++)
  126. {
  127. for (int y = 0; y < map.width; y++)
  128. {
  129. (*this)[x][y]->pos = Position(x, y);
  130. (*this)[x][y]->data = *map[x][y];
  131. (*this)[x][y]->calcWeight();
  132. }
  133. }
  134. for (int x = 0; x < map.height; x++)
  135. {
  136. for (int y = 0; y < map.width; y++)
  137. {
  138. int e = 0;
  139. for (int i = 0; i < 8; i++)
  140. {
  141. int xn = x + direction[i][0];
  142. int yn = y + direction[i][1];
  143. if (xn > 0 && xn < map.height)
  144. {
  145. if (yn > 0 && yn < map.width)
  146. {
  147. (*this)[x][y]->branches[e] = (*this)[xn][yn];
  148. e += 1;
  149. (*this)[x][y]->dirCount += 1;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. ///////////
  157. void calcdijkstra(Position start)
  158. {
  159. priority_queue <Node*, vector<Node*>, fo_sort> nodesToVisit;
  160. Node *current = (*this)[start.x][start.y];
  161. this->start = start;
  162. current->distation = 0;
  163. nodesToVisit.push(current);
  164. while (!nodesToVisit.empty())
  165. {
  166. current = nodesToVisit.top();
  167. nodesToVisit.pop();
  168. current->vizited = 1;
  169. int n = current->dirCount;
  170. for (int i = 0; i < n; i++)
  171. {
  172. if (current->branches[i]->vizited != 1)
  173. {
  174. if (current->branches[i]->calcDistation(*current))
  175. nodesToVisit.push(current->branches[i]);
  176. }
  177. else
  178. {
  179. //cout << "All good!";
  180. }
  181. }
  182. }
  183. }
  184. Node min(Node current)
  185. {
  186. float d = 1000000000.0;
  187. Node next;
  188. for (int i = 0; i < 8; i++)
  189. {
  190. if (current.branches[i]->distation <= d)
  191. {
  192. next = *current.branches[i];
  193. d = current.branches[i]->distation;
  194. }
  195. }
  196. return next;
  197. }
  198. BMP drowRoad(Position end)
  199. {
  200. int x = end.x;
  201. int y = end.y;
  202. Node current = *(*this)[x][y];
  203. while (current.distation != 0)
  204. {
  205. current = min(current);
  206. if (map[current.pos.x][current.pos.y]->b < 20 &&
  207. map[current.pos.x][current.pos.y]->g < 20 &&
  208. map[current.pos.x][current.pos.y]->r < 20)
  209. throw "The rok is very cool! I can't go there! Sorry o-o I'm not a sportsman!";
  210. map[current.pos.x][current.pos.y]->b = 0;
  211. map[current.pos.x][current.pos.y]->g = 255;
  212. map[current.pos.x][current.pos.y]->r = 0;
  213. }
  214. map[start.x][start.y]->b = 0;
  215. map[start.x][start.y]->g = 0;
  216. map[start.x][start.y]->r = 255;
  217. map[end.x][end.y]->b = 0;
  218. map[end.x][end.y]->g = 0;
  219. map[end.x][end.y]->r = 255;
  220. return map;
  221. }
  222. };