Dzham

Untitled

Apr 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <unordered_map>
  6. #include <unordered_set>
  7. #include <string>
  8. #include <set>
  9. #include <queue>
  10.  
  11. class Graph {
  12. private:
  13. int size_g;
  14. std::vector<std::vector<int>> data;
  15. std::vector<int> fleas;
  16. std::vector<int> used;
  17. std::vector<std::vector<int>> coordinates;
  18. int size_H;
  19. int size_W;
  20.  
  21. public:
  22. std::vector<bool> isused;
  23. std::vector<int> distance;
  24.  
  25. explicit Graph(int n) {
  26. data.resize(n);
  27. isused.resize(n);
  28. size_g = n;
  29. }
  30.  
  31. Graph(int h, int w) {
  32. size_g = h * w;
  33. size_H = h;
  34. size_W = w;
  35. isused.resize(h * w);
  36. coordinates.resize(h * w);
  37. int counter = 0;
  38. for (int i = 0; i < h; i++) {
  39. for (int j = 0; j < w; j++) {
  40. coordinates[counter].push_back(i);
  41. coordinates[counter].push_back(j);
  42. counter++;
  43. }
  44. }
  45. }
  46.  
  47. void insert(int a, int b) {
  48. if (a != b) {
  49. data[a - 1].push_back(b);
  50. data[b - 1].push_back(a);
  51. }
  52. }
  53.  
  54. explicit Graph(std::vector<std::vector<int>> graph) {
  55. isused.resize(graph.size());
  56. size_g = graph.size();
  57. data = graph;
  58. }
  59.  
  60. void printgraph() {
  61. for (int i = 0; i < data.size(); i++) {
  62. for (auto j : data[i]) {
  63. std::cout << j << " ";
  64. }
  65. std::cout << '\n';
  66. }
  67. }
  68.  
  69. std::vector<int> neighbours(int a) {
  70. int y = coordinates[a][0];
  71. int x = coordinates[a][1];
  72. std::vector<int> result;
  73. int n1 = a + 2 * size_W + 1;
  74. int n2 = a + 2 * size_W - 1;
  75. int n3 = a - 2 * size_W + 1;
  76. int n4 = a - 2 * size_W - 1;
  77. int n5 = a + size_W + 2;
  78. int n6 = a + size_W - 2;
  79. int n7 = a - size_W + 2;
  80. int n8 = a - size_W - 2;
  81. bool check1 = y + 2 < size_H && x + 1 < size_W;
  82. bool check2 = y + 2 < size_H && x - 1 > -1;
  83. bool check3 = y - 2 > -1 && x + 1 < size_W;
  84. bool check4 = y - 2 > -1 && x - 1 > -1;
  85. bool check5 = y + 1 < size_H && x + 2 < size_W;
  86. bool check6 = y + 1 < size_H && x - 2 > -1;
  87. bool check7 = y - 1 > -1 && x + 2 < size_W;
  88. bool check8 = y - 1 > -1 && x - 2 > -1;
  89. if (check1 && !isused[n1]) {
  90. result.push_back(n1);
  91. }
  92. if (check2 && !isused[n2]) {
  93. result.push_back(n2);
  94. }
  95. if (check3 && !isused[n3]) {
  96. result.push_back(n3);
  97. }
  98. if (check4 && !isused[n4]) {
  99. result.push_back(n4);
  100. }
  101. if (check5 && !isused[n5]) {
  102. result.push_back(n5);
  103. }
  104. if (check6 && !isused[n6]) {
  105. result.push_back(n6);
  106. }
  107. if (check7 && !isused[n7]) {
  108. result.push_back(n7);
  109. }
  110. if (check8 && !isused[n8]) {
  111. result.push_back(n8);
  112. }
  113. return result;
  114. }
  115.  
  116. int bfschess(int start, std::vector<int> fleas) {
  117. std::queue<int> vert;
  118. std::vector<int> result(size_g, -1);
  119. vert.push(start);
  120. isused[start] = true;
  121. result[start] = 0;
  122. while (vert.size() != 0) {
  123. int s = vert.front();
  124. vert.pop();
  125. std::vector<int> child = neighbours(s);
  126. for (auto into : child) {
  127. if (!isused[into]) {
  128. isused[into] = true;
  129. vert.push(into);
  130. result[into] = result[s] + 1;
  131. }
  132. }
  133. }
  134. int answer = 0;
  135. for (auto i : fleas) {
  136. if (result[i] == -1) {
  137. answer = -1;
  138. break;
  139. } else {
  140. answer += result[i];
  141. }
  142. }
  143. return answer;
  144. }
  145.  
  146. void Dijkstra(int a, int b) {
  147. isused.clear();
  148. isused.resize(size_g);
  149. isused[a - 1] = true;
  150. std::vector<int> result;
  151. std::vector<int> way;
  152. std::vector<std::vector<int>> ways;
  153. ways.resize(size_g);
  154. result = data[a - 1];
  155. int v;
  156. for (int i = 0; i < size_g; i++) {
  157. if (result[i] != 200000) {
  158. ways[i].push_back(i + 1);
  159. }
  160. }
  161. for (int j = 0; j < size_g; j++) {
  162. v = -1;
  163. for (int i = 0; i < size_g; i++) {
  164. if (!isused[i] && ((v == -1) || result[v] > result[i])) {
  165. v = i;
  166. }
  167. }
  168. if (v == -1) {
  169. v = size_g - 1;
  170. }
  171. isused[v] = true;
  172. for (int i = 0; i < size_g; i++) {
  173. if (result[i] > result[v] + data[i][v]) {
  174. ways[i].clear();
  175. ways[i].insert(ways[i].end(), ways[v].begin(), ways[v].end());
  176. ways[i].push_back(i + 1);
  177. result[i] = result[v] + data[v][i];
  178. }
  179. }
  180. }
  181. if (result[b - 1] <= 0 || result[b - 1] == 200000) {
  182. std::cout << -1;
  183. } else {
  184. std::cout << result[b - 1] << "\n";
  185. std::cout << a;
  186. for (auto i : ways[b - 1]) {
  187. std::cout << " " << i;
  188. }
  189. std::cout << '\n';
  190. }
  191. }
  192. };
  193.  
  194. int main() {
  195. int N, M, S, T, Q;
  196. int a, b;
  197. std::cin >> N >> M >> S >> T >> Q;
  198. std::vector<int> fleas;
  199. for (int i = 0; i < Q; i++) {
  200. std::cin >> a >> b;
  201. fleas.push_back((a - 1) * M + b - 1);
  202. }
  203. Graph graph(N, M);
  204. std::cout << graph.bfschess((S - 1) * M + (T - 1), fleas);
  205. }
Add Comment
Please, Sign In to add comment