F - Prime Path POJ - 3126
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.
— 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.
1033The 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.
1733
3733
3739
3779
8779
8179
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
VOJ里C++编译器sqrt里面不可以放int、不可以用to_string、stoi。CE了好几次
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
//#include <unordered_set>
//#include <unordered_map>
//#include <xfunctional>
#define ll long long
#define PII pair<int, int>
using namespace std;
int dir[5][2] = { {0,1} ,{0,-1}, {1,0}, {-1,0} ,{0,0} };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int maxn = 1e5;
//if(x<0 || x>=r || y<0 || y>=c)
struct node
{
int cnt, val;
};
int replace(int num,int n,int token)
{
int arr[4];
arr[0] = num / 1000;
arr[1] = (num / 100)%10;
arr[2] = (num / 10)%10;
arr[3] = num % 10;
arr[n] = token;
int res=arr[0]*1000+arr[1]*100+arr[2]*10+arr[3];
return res;
}
bool isprime(int num)
{
if (num == 2 || num == 3)
return 1;
if (num % 6 != 1 && num % 6 != 5)
return 0;
double nums = num;
int tmp = sqrt(nums);
for (int i = 5; i <= tmp; i += 6)
if (num %i == 0 || num % (i + 2) == 0)
return 0;
return 1;
}
int main()
{
int t;
cin >> t;
int visited[maxn];
while (t--)
{
memset(visited, 0, sizeof(visited));
int a, b;
cin >> a >> b;
queue<node> que;
node a1;
a1.cnt = 0;
a1.val = a;
que.push(a1);
visited[a] = 1;
while (!que.empty())
{
node front = que.front();
que.pop();
if (front.val == b)
{
cout << front.cnt<<endl;
break;
}
for (int n = 0; n < 4; n++)
{
for (int i = 0; i < 10; i++)
{
if (n == 0 && i == 0)
continue;
int tmp = replace(front.val, n, i);
if (visited[tmp]==0)
{
visited[tmp] = 1;
if (isprime(tmp))
{
node t;
t.cnt = front.cnt + 1;
t.val = tmp;
que.push(t);
}
}
}
}
}
}
return 0;
}