Prime Path
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don't know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on... Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
Input
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
Source
题目类型:素数+BFS
算法分析:首先将四位数的所有素数找出来,然后从开始的素数每次进行一次BFS搜索,转移的条件是下一个素数的与当前素数只有一位不同,注意每次判断素数的范围是1000~9999!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 10000 + 66; struct Node { int v, s; Node () {} Node (int vv, int ss) : v (vv), s (ss) {} }; bool prime[maxn+66], v[maxn+66]; long long res; int aa[16], lena, bb[16], lenb; bool Ok (int a, int b) { lena = 0; do { aa[lena++] = a % 10; a /= 10; } while (a); lenb = 0; do { bb[lenb++] = b % 10; b /= 10; } while (b); int cnt = 0; for (int i = 0; i < lena; i++) if (aa[i] != bb[i]) cnt++; if (cnt == 1) return true; return false; } bool bfs (int a, int b) { res = 0; memset (v, false, sizeof (v)); v[a] = true; queue <Node> qu; qu.push(Node (a, 0)); while (!qu.empty ()) { Node tt = qu.front(); qu.pop(); if (tt.v == b) { res = tt.s; return true; } for (int i = 1000; i < 10000; i++) { if (prime[i] && !v[i] && Ok (tt.v, i)) { v[i] = true; qu.push(Node (i, tt.s + 1)); } } } return false; } void Get () { memset (prime, true, sizeof (prime)); for (int i = 2; i <= maxn; i++) { if (prime[i]) { for (int j = 2; j * i <= maxn; j++) prime[j*i] = false; } } } int main() { // ifstream cin ("aaa.txt"); Get (); int t; cin >> t; while (t--) { int a, b; cin >> a >> b; if(a > b) swap (a, b); if (bfs (a, b)) cout << res << endl; else cout << "Impossible" << endl; } return 0; } |
- « 上一篇:poj3122
- poj3233:下一篇 »