题目链接:Prime Path POJ - 3126
===================================================
Prime Path
Time Limit: 1000MS
Memory Limit: 65536K
Description
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:Northwestern Europe 2006
===================================================
算法:BFS
知识补充:求质数
- 方法1:求单个数是否为质数——质数单个求就是直接从2到sqrt(n)进行求余
bool check(int x){
for(int i=2;i*i<=x;i++) if(x%i==0) return false;
return true;
}
- 方法2:筛选方法的话——就是从2到sqrt(n)取i,范围(最大到n),然后进行向后乘i
void isPrime(int n){
memset(ip,true,sizeof ip);ip[0] = false,ip[1] = false;
double db = n;
int m = floor(sqrt(db));
for(int i = 2;i<=m;i++) if(ip[i])
for(int j = i;j*i <=n; j++) ip[j*i] = false;
}
思路:
- 分别通过两种方法进行BFS
===================================================
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int n,m;
int y[5];
int ans[10000];
queue<int> q;
bool check(int x){
for(int i=2;i*i<=x;i++) if(x%i==0) return false;
return true;
}
void go(int x){
for(int i=1;i<=4;i++){
y[i]=x%10;
x/=10;
}
}
void dfs(){
memset(ans,-1,sizeof ans);
q.push(n);ans[n]=0;
while(!q.empty()){
int x = q.front();q.pop();
if(x==m) break;
go(x);
int z = x - y[1];
for(int i=0;i<=9;i++) if(i!=y[1]&&ans[z+i]<0&&check(z+i)) q.push(z+i),ans[z+i]=ans[x]+1;
z = x - y[2]*10;
for(int i=0;i<=9;i++) if(i!=y[2]&&ans[z+i*10]<0&&check(z+i*10)) q.push(z+i*10),ans[z+i*10]=ans[x]+1;
z = x - y[3]*100;
for(int i=0;i<=9;i++) if(i!=y[3]&&ans[z+i*100]<0&&check(z+i*100)) q.push(z+i*100),ans[z+i*100]=ans[x]+1;
z = x - y[4]*1000;
for(int i=1;i<=9;i++) if(i!=y[4]&&ans[z+i*1000]<0&&check(z+i*1000)) q.push(z+i*1000),ans[z+i*1000]=ans[x]+1;
}
while(!q.empty()) q.pop();
}
int main()
{
int _;cin>>_;
while(_--){
cin>>n>>m;
dfs();
if(ans[m]>=0) cout<<ans[m]<<endl;
else cout<<"Impossible"<<endl;
}
return 0;
}
===================================================
#include <iostream>
#include <cstring>
#include <math.h>
#include <queue>
using namespace std;
const int M = 1e5;
int ans;
bool ip[M+5];
int dp[M+5];
void isPrime(int n){
memset(ip,true,sizeof ip);ip[0] = false,ip[1] = false;
double db = n;
int m = floor(sqrt(db));
for(int i = 2;i<=m;i++) if(ip[i])
for(int j = i;j*i <=n; j++) ip[j*i] = false;
}
void init(){
ans = 0;
memset(dp,-1,sizeof dp);
}
int bfs(int a,int b){
queue<int> q;
q.push(a);dp[a] = 0;
while(!q.empty()){
int p = q.front();q.pop();
if(p == b) return dp[b];
else{
int x1 = p%10, y1 = p %100, z1 = p%1000;
int x = p - x1, y = p - y1 + x1 , z = p - z1 + y1 ;
for(int i=0;i<=9;i++){
int h = x+i;if(dp[h]==-1&&ip[h]) dp[h] = dp[p] + 1,q.push(h);
h = y+i*10;if(dp[h]==-1&&ip[h]) dp[h] = dp[p] + 1,q.push(h);
h = z+i*100;if(dp[h]==-1&&ip[h]) dp[h] = dp[p] + 1,q.push(h);
if(i==0) continue;
h = z1 + i*1000;if(dp[h]==-1&&ip[h]) dp[h] = dp[p] + 1,q.push(h);
}
}
}
return dp[b];
}
int main()
{
int _;cin>>_;
isPrime(M);
while(_--){
init();
int a,b;cin>>a>>b;
cout<<bfs(a,b)<<endl;
}
return 0;
}