p12101.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <algorithm>
  2. #include <array>
  3. #include <iostream>
  4. #include <map>
  5. #include <utility>
  6. #include <vector>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <climits>
  10. #include <queue>
  11. using namespace std;
  12. typedef unsigned int nat;
  13. struct Edge;
  14. struct Node;
  15. struct Node
  16. {
  17. Node(nat id) : id{id} {}
  18. nat id;
  19. vector<Edge*> edges;
  20. };
  21. struct Edge
  22. {
  23. Edge(Node *a, Node *b) : a{a}, b{b}
  24. {
  25. a->edges.push_back(this);
  26. b->edges.push_back(this);
  27. }
  28. Node *other(Node *curr) const
  29. {
  30. if(curr == a)
  31. return b;
  32. return a;
  33. }
  34. Node *a;
  35. Node *b;
  36. };
  37. vector<bool> prime_sieve(nat upper_limit)
  38. {
  39. ++upper_limit;
  40. vector<bool> primes(upper_limit, true);
  41. primes[0] = false;
  42. primes[1] = false;
  43. for(nat i = 2; i < upper_limit; ++i)
  44. {
  45. if(!primes[i])
  46. continue;
  47. for(nat j = 2*i; j < upper_limit; j += i)
  48. {
  49. primes[j] = false;
  50. }
  51. }
  52. return primes;
  53. }
  54. bool neighbours(Node *a, Node *b)
  55. {
  56. nat a_val = a->id, b_val = b->id;
  57. nat diffs = 0;
  58. for(nat i = 0; i < 4; ++i)
  59. {
  60. if((a_val% 10) != (b_val% 10))
  61. {
  62. ++diffs;
  63. }
  64. a_val /= 10;
  65. b_val /= 10;
  66. }
  67. return (diffs == 1);
  68. }
  69. Node *get_by_id(vector<Node*> const &graph, nat id)
  70. {
  71. for(auto n : graph)
  72. {
  73. if(n->id == id)
  74. return n;
  75. }
  76. return nullptr;
  77. }
  78. vector<Node*> make_graph(const vector<bool> &primes)
  79. {
  80. vector<Node*> graph(primes.size());
  81. for(nat i = 0; i < primes.size(); ++i)
  82. {
  83. graph[i] = new Node(i);
  84. }
  85. for(nat i = 1001; i < primes.size(); i += 2)
  86. {
  87. if(!primes[i])
  88. continue;
  89. for(nat j = i+2; j < primes.size(); j += 2)
  90. {
  91. if(primes[j] && neighbours(graph[i], graph[j]))
  92. {
  93. new Edge(graph[i], graph[j]);
  94. }
  95. }
  96. }
  97. return graph;
  98. }
  99. nat BFS_len(Node *source, Node *target)
  100. {
  101. queue<pair<Node*, nat>> q;
  102. q.push(make_pair(source, 0));
  103. vector<bool> visited(10001, false);
  104. visited[source->id] = true;
  105. while(!q.empty())
  106. {
  107. Node *curr = q.front().first;
  108. nat depth = q.front().second;
  109. q.pop();
  110. if(curr == target)
  111. {
  112. return depth;
  113. }
  114. for(Edge *e : curr->edges)
  115. {
  116. Node *other = e->other(curr);
  117. if(!visited[other->id])
  118. {
  119. q.push(make_pair(other, depth+1));
  120. visited[other->id] = true;
  121. }
  122. }
  123. }
  124. return (nat)-1;
  125. }
  126. int main()
  127. {
  128. vector<bool> primes = prime_sieve(10000);
  129. vector<Node*> graph = make_graph(primes);
  130. nat num_tests, source, target;
  131. /*
  132. while(cin >> source)
  133. {
  134. for(auto e : graph[source]->edges)
  135. {
  136. cout << e->other(graph[source])->id << endl;
  137. }
  138. }
  139. return 0;
  140. /*/
  141. /*
  142. while(cin >> source)
  143. {
  144. cout << primes[source] << endl;
  145. }
  146. return 0;
  147. //*/
  148. cin >> num_tests;
  149. for(nat i = 0; i < num_tests; ++i)
  150. {
  151. cin >> source >> target;
  152. nat len = BFS_len(graph[source], graph[target]);
  153. if(len == (nat)-1)
  154. {
  155. cout << "Impossible" << endl;
  156. }
  157. else
  158. {
  159. cout << len << endl;
  160. }
  161. }
  162. return 0;
  163. }