Advertisement
iStrzalka

Untitled

Oct 11th, 2017
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8. int self = 10000000;
  9.  
  10. bool gora = false;
  11. bool dol = false;
  12. bool prawo = false;
  13. bool lewo = false;
  14.  
  15. bool finish = false;
  16. };
  17.  
  18. node graph[502][502];
  19.  
  20.  
  21. void BFS(int START_X, int START_Y, int kierunek)
  22. {
  23. graph[START_X][START_Y].self = 0;
  24. queue<pair<int,int > > kolejka;
  25. queue<int> strzalki;
  26. kolejka.push({START_X,START_Y});
  27. strzalki.push(kierunek);
  28.  
  29.  
  30.  
  31. int cur_x, cur_y, cur_str, licznik;
  32. while(!kolejka.empty())
  33. {
  34. cur_x = kolejka.front().first;
  35. cur_y = kolejka.front().second;
  36. cur_str = strzalki.front();
  37.  
  38. cout << cur_x << " " << cur_y << endl;
  39.  
  40. kolejka.pop();
  41. strzalki.pop();
  42.  
  43. licznik = 0;
  44. if(graph[cur_x][cur_y].lewa)
  45. licznik++;
  46. if(graph[cur_x][cur_y].prawa)
  47. licznik++;
  48. if(graph[cur_x][cur_y].dol)
  49. licznik++;
  50. if(graph[cur_x][cur_y].gora)
  51. licznik++;
  52.  
  53. if(licznik == 2)
  54. {
  55. if(cur_str == 1)
  56. {
  57. if(!graph[cur_x][cur_y].gora)
  58. {
  59.  
  60. }
  61. }
  62. if(cur_str == 2)
  63. {
  64. if(!graph[cur_x][cur_y].dol)
  65. {
  66.  
  67. }
  68. }
  69. if(cur_str == 3)
  70. {
  71. if(!graph[cur_x][cur_y].prawo)
  72. {
  73.  
  74. }
  75. }
  76. if(cur_str == 4)
  77. {
  78. if(!graph[cur_x][cur_y].lewa)
  79. {
  80.  
  81. }
  82. }
  83. }
  84.  
  85.  
  86. //dol
  87. if(graph[cur_x][cur_y].dol)
  88. {
  89. if(cur_str == 1)
  90. {
  91. if(graph[cur_x][cur_y].self < graph[cur_x+1][cur_y].self)
  92. {
  93. graph[cur_x+1][cur_y].self = graph[cur_x][cur_y].self;
  94. kolejka.push({cur_x+1, cur_y});
  95. strzalki.push(1);
  96. }
  97. }
  98. else
  99. {
  100. if(graph[cur_x][cur_y].self + 1 < graph[cur_x+1][cur_y].self)
  101. {
  102. kolejka.push({cur_x+1, cur_y});
  103. strzalki.push(1);
  104. graph[cur_x+1][cur_y].self = graph[cur_x][cur_y].self + 1;
  105. }
  106. }
  107. }
  108.  
  109. //gora
  110. if(graph[cur_x][cur_y].gora)
  111. {
  112. if(cur_str == 2)
  113. {
  114. if(graph[cur_x][cur_y].self < graph[cur_x-1][cur_y].self)
  115. {
  116. graph[cur_x-1][cur_y].self = graph[cur_x][cur_y].self;
  117. kolejka.push({cur_x-1, cur_y});
  118. strzalki.push(2);
  119. }
  120. }
  121. else
  122. {
  123. if(graph[cur_x][cur_y].self + 1 < graph[cur_x-1][cur_y].self)
  124. {
  125. kolejka.push({cur_x-1, cur_y});
  126. strzalki.push(2);
  127. graph[cur_x-1][cur_y].self = graph[cur_x][cur_y].self + 1;
  128. }
  129. }
  130. }
  131.  
  132. //prawo
  133. if(graph[cur_x][cur_y].prawo)
  134. {
  135. if(cur_str == 3)
  136. {
  137. if(graph[cur_x][cur_y].self < graph[cur_x][cur_y+1].self)
  138. {
  139. graph[cur_x][cur_y].self = graph[cur_x][cur_y+1].self;
  140. kolejka.push({cur_x, cur_y+1});
  141. strzalki.push(3);
  142. }
  143. }
  144. else
  145. {
  146. if(graph[cur_x][cur_y].self + 1 < graph[cur_x][cur_y+1].self)
  147. {
  148. kolejka.push({cur_x, cur_y+1});
  149. strzalki.push(3);
  150. graph[cur_x][cur_y+1].self = graph[cur_x][cur_y].self + 1;
  151. }
  152. }
  153. }
  154.  
  155. if(graph[cur_x][cur_y].lewo)
  156. {
  157. if(cur_str == 4)
  158. {
  159. if(graph[cur_x][cur_y].self < graph[cur_x][cur_y-1].self)
  160. {
  161. graph[cur_x][cur_y-1].self = graph[cur_x][cur_y].self;
  162. kolejka.push({cur_x, cur_y-1});
  163. strzalki.push(4);
  164. }
  165. }
  166. else
  167. {
  168. if(graph[cur_x][cur_y].self + 1 < graph[cur_x][cur_y-1].self)
  169. {
  170. kolejka.push({cur_x, cur_y-1});
  171. strzalki.push(4);
  172. graph[cur_x][cur_y-1].self = graph[cur_x][cur_y].self + 1;
  173. }
  174. }
  175. }
  176. }
  177. }
  178.  
  179.  
  180. int main()
  181. {
  182. int n, m, kier;
  183. string wej;
  184. string tab;
  185. string::iterator found;
  186. cin >> n >> m >> wej;
  187.  
  188. if(wej == "DOL")
  189. kier = 1;
  190. if(wej == "GORA")
  191. kier = 2;
  192. if(wej == "PRAWO")
  193. kier = 3;
  194. if(wej == "LEWO")
  195. kier = 4;
  196.  
  197. int start_x, start_y, finish_x, finish_y;
  198. for(int i = 0; i < 2*n+1; i++)
  199. {
  200. cin >> tab;
  201. if(i == 0 || i == 2*n+1)
  202. continue;
  203.  
  204. found = find(tab.begin(), tab.end(), 'S');
  205. if(found != tab.end())
  206. {
  207. start_x = i/2;
  208. start_y = (found - tab.begin())/2;
  209. }
  210. found = find(tab.begin(), tab.end(), 'R');
  211. if(found != tab.end())
  212. {
  213. finish_x = i/2;
  214. finish_y = (found - tab.begin())/2;
  215. }
  216.  
  217. if(i % 2 == 1)
  218. {
  219. for(int j = 2; j < 2*m+1; j += 2)
  220. {
  221. if(tab[j] == '.')
  222. {
  223. graph[i/2][j/2 - 1].prawo = true;
  224. graph[i/2][j/2].lewo = true;
  225. }
  226. }
  227. }
  228. if(i % 2 == 0)
  229. {
  230. for(int j = 1; j < 2*m+1; j += 2)
  231. {
  232. if(tab[j] == '.')
  233. {
  234. graph[i/2 - 1][j/2].dol = true;
  235. graph[i/2][j/2].gora = true;
  236. }
  237.  
  238. }
  239. }
  240. }
  241.  
  242. cout << endl << "l p g d";
  243. cout << endl << endl;
  244. for(int i = 0; i < n; i++)
  245. {
  246. for(int j = 0; j < m; j++)
  247. {
  248. cout << graph[i][j].lewo << graph[i][j].prawo <<graph[i][j].gora <<graph[i][j].dol << " ";
  249. }
  250. cout << endl;
  251. }
  252. BFS(start_x, start_y,kier);
  253. cout << graph[finish_x][finish_y].self;
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement