p12101.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. vector<Node*> make_graph(const vector<bool> &primes)
  70. {
  71. vector<Node*> graph(primes.size());
  72. for(nat i = 0; i < primes.size(); ++i)
  73. {
  74. graph[i] = new Node(i);
  75. }
  76. for(nat i = 1001; i < primes.size(); i += 2)
  77. {
  78. if(!primes[i])
  79. continue;
  80. for(nat j = i+2; j < primes.size(); j += 2)
  81. {
  82. if(neighbours(graph[i], graph[j]))
  83. {
  84. new Edge(graph[i], graph[j]);
  85. }
  86. }
  87. }
  88. return graph;
  89. }
  90. nat BFS_len(Node *source, Node *target)
  91. {
  92. queue<pair<Node*, nat>> q;
  93. q.push(make_pair(source, 0));
  94. vector<bool> visited(10001, false);
  95. visited[source->id] = true;
  96. while(!q.empty())
  97. {
  98. Node *curr = q.front().first;
  99. nat depth = q.front().second;
  100. q.pop();
  101. if(curr == target)
  102. {
  103. return depth;
  104. }
  105. for(Edge *e : curr->edges)
  106. {
  107. Node *other = e->other(curr);
  108. if(!visited[other->id])
  109. {
  110. q.push(make_pair(other, depth+1));
  111. visited[other->id] = true;
  112. }
  113. }
  114. }
  115. return (nat)-1;
  116. }
  117. int main()
  118. {
  119. vector<bool> primes = prime_sieve(10000);
  120. vector<Node*> graph = make_graph(primes);
  121. nat num_tests, source, target;
  122. /*
  123. while(cin >> source)
  124. {
  125. for(auto e : graph[source]->edges)
  126. {
  127. cout << e->other(graph[source])->id << endl;
  128. }
  129. }
  130. return 0;
  131. */
  132. cin >> num_tests;
  133. for(nat i = 0; i < num_tests; ++i)
  134. {
  135. cin >> source >> target;
  136. nat len = BFS_len(graph[source], graph[target]);
  137. if(len == (nat)-1)
  138. {
  139. cout << "Impossible" << endl;
  140. }
  141. else
  142. {
  143. cout << len << endl;
  144. }
  145. }
  146. return 0;
  147. }