您的位置:首页 > 编程语言

哈理工练习赛 杭电 HDU Prime Path 1973 poj 3126 Prime Path

2015-11-29 21:39 423 查看
C - Prime Path
Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u
SubmitStatusPracticePOJ
3126

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

搜索思路没什么难的。难点在于如何去枚举四位数中每位数的值。
/*=============================================================================
#
#      Author: liangshu - cbam
#
#      QQ : 756029571
#
#      School : 哈尔滨理工大学
#
#      Last modified: 2015-11-29 21:40
#
#     Filename: H.cpp
#
#     Description:
#        The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
int primer[10000];
int s, e;
int vis[10003];
struct Node{
int x, s;
Node(int x, int s):x(x), s(s){}
};
void prime(){
memset(primer, 0, sizeof(primer));
for(int i = 2; i <= sqrt(10000); i++){
if(!primer[i]){
for(int j = i * i; j <= 10000; j+=i){
primer[j] = 1;
}
}
}
}
void bfs(int x){
memset(vis, 0, sizeof(vis));
queue<Node>q;
vis[x] = 1;
q.push(Node(x, 0));
int flag = 0;
while(!q.empty()){
Node u = q.front();
q.pop();
if(u.x == e){
flag = 1;
printf("%d\n", u.s);return ;
}
for(int i = 0; i < 4; i++){
int k = u.x % int(pow(10, i+ 1)+ 0.3);
for(int j = 0; j <= 9; j++){
if(k + int(j * pow(10, i) + 0.3) < int(pow(10, i + 1) + 0.3 )){
int tb = u.x + int(j * pow(10, i) + 0.3);
if(!primer[tb] && !vis[tb] && tb >= 1000){
q.push(Node(tb, u.s + 1));
vis[tb] = 1;
}

}
if(k - int(j * pow(10, i) + 0.3) >= 0){
int tb = u.x - int(j * pow(10, i) + 0.3);
if(!primer[tb] && !vis[tb]&& tb >= 1000){
vis[tb] = 1;
q.push(Node(tb, u.s + 1));
}

}
}
}
}
if(!flag){
printf("Impossible\n");return ;
}
}

int main()
{
int t;scanf("%d", &t);
while(t--){
prime();
scanf("%d%d", &s, &e);
bfs(s);
}
return 0;
}
/*
23
6101 6113
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编程 bfs 哈理工 杭电