POJ3126 Prime Path —— BFS + 素数表

来源:互联网 发布:李洪成排盘软件下载 编辑:程序博客网 时间:2024/05/19 06:48

题目链接:http://poj.org/problem?id=3126


Prime Path
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 22936 Accepted: 12706

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

31033 81791373 80171033 1033

Sample Output

670

Source

Northwestern Europe 2006



题解:

1.打印素数表。

2.由于只有四位数,直接枚举不会超时。由于要求的是“最少步数”,所以用BFS进行搜索。




代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <set>#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 5+10;struct node //num为素数,dig[]为这个素数的每个位上的数,便于操作。{    int num, dig[4], step;};int vis[100010], pri[100010];queue<node>que;int bfs(node s, node e){    ms(vis,0);    while(!que.empty()) que.pop();    s.step = 0;    vis[s.num] = 1;    que.push(s);    node now, tmp;    while(!que.empty())    {        now = que.front();        que.pop();        if(now.num==e.num)            return now.step;        for(int i = 0; i<4; i++)    //枚举位数        for(int j = 0; j<10; j++)   //枚举数字        {            if(i==3 && j==0) continue;  //首位不能为0            tmp = now;            tmp.dig[i] = j;     //第i为变为j            tmp.num = tmp.dig[0] + tmp.dig[1]*10+tmp.dig[2]*100+tmp.dig[3]*1000;            if(!pri[tmp.num] && !vis[tmp.num])  //num为素数并且没有被访问            {                vis[tmp.num] = 1;                tmp.step++;                que.push(tmp);            }        }    }    return -1;}void init()     //素数表,pri[]==0的为素数{    int m = sqrt(100000+0.5);    ms(pri,0);    pri[1] = 1;    for(int i = 2; i<=m; i++) if(!pri[i])        for(int j = i*i; j<=100000; j += i)            pri[j] = 1;}int main(){    init();    int T;    scanf("%d",&T);    while(T--)    {        int n, m;        node s, e;        scanf("%d%d",&n,&m);        s.num = n; e.num = m;        for(int i = 0; i<4; i++, n /= 10) s.dig[i] = n%10;        for(int i = 0; i<4; i++, m /= 10) e.dig[i] = m%10;        int ans = bfs(s,e);        if(ans==-1)            puts("Impossible");        else            printf("%d\n",ans);    }}



另外一种写法:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <set>#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 5+10;struct node{    int num, step;};int vis[100010], dig[5];int pri[100010];queue<node>que;int bfs(int s, int e){    ms(vis,0);    while(!que.empty()) que.pop();    node now, tmp;    now.num = s;    now.step = 0;    vis[now.num] = 1;    que.push(now);    while(!que.empty())    {        now = que.front();        que.pop();        if(now.num==e)            return now.step;        dig[0] = now.num%10;        dig[1] = (now.num/10)%10;        dig[2] = (now.num/100)%10;        dig[3] = (now.num/1000)%10;        for(int i = 0; i<4; i++)        for(int j = 0; j<10; j++)        {            if(i==3 && j==0) continue;            tmp.num = now.num + (j- dig[i])*pow(10,i);  //pow前面不要加上强制类型(int)//            tmp.num = now.num + (j- dig[i])* (int)pow(10,i);//            (int)pow(10,2) 居然等于99, 看来还是不要依赖这些函数            if(!pri[tmp.num] && !vis[tmp.num])            {                vis[tmp.num] = 1;                tmp.step = now.step + 1;                que.push(tmp);            }        }    }    return -1;}void init(){    int m = sqrt(100000+0.5);    ms(pri,0);    pri[1] = 1;    for(int i = 2; i<=m; i++) if(!pri[i])        for(int j = i*i; j<=100000; j += i)            pri[j] = 1;}int main(){    init();    int T;    scanf("%d",&T);    while(T--)    {        int n, m;        scanf("%d%d",&n,&m);        int ans = bfs(n,m);        if(ans==-1)            puts("Impossible");        else            printf("%d\n",ans);    }}