Prime Path bfs搞定

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

一看这道题就想到bfs

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
using namespace std;
class que
{
    public:
    int d;
    int times;
}temp;
int vis[9000],sta,time,num;
int main()
{
    int prime[10000]={1,1,0,0};
    for(int i=2;i<=9999;i++)
    {
        if(!prime[i])
        {
            for(int j=i;j*i<=9999;j++)
                prime[i*j]=1;
        }
    }
    int T,a,b;
    queue<que>q;
    cin>>T;
    while(T--)
    {
        cin>>a>>b;
        int flag=0;
        memset(vis,0,sizeof(vis));
        while(!q.empty())q.pop();
        temp.d=a,temp.times=0;
        q.push(temp);
        vis[a-1000]=1;
        if(!prime[a]&&!prime[b])
        while(!q.empty())
        {
            if(q.front().d==b)
            {
                flag=1;
                cout<<q.front().times<<endl;
                break;
            }
            sta=q.front().d,time=q.front().times;
            for(int i=1000;i>0;i/=10)
            {
                for(int j=0;j<10;j++)
                {
                    num=sta-sta/i%10*i+i*j;
                    if(num<1000||prime[num]||vis[num-1000])continue;
                    vis[num-1000]=1;
                    temp.d=num,temp.times=time+1;
                    q.push(temp);
                }
            }
            q.pop();
        }
        if(!flag)cout<<"impossible"<<endl;
    }
}
时间: 2025-01-15 10:13:08

Prime Path bfs搞定的相关文章

poj 3126 Prime Path (bfs)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13813   Accepted: 7796 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-dig

POJ 3126 Prime Path(BFS 数字处理)

题意  给你两个4位素数a, b  你每次可以改变a的一位数但要求改变后仍为素数  求a至少改变多少次才能变成b 基础的bfs  注意数的处理就行了  出队一个数  然后入队所有可以由这个素数经过一次改变而来的素数  知道得到b #include <cstdio> #include <cstring> using namespace std; const int N = 10000; int p[N], v[N], d[N], q[N], a, b; void initPrime(

[POJ]P3126 Prime Path[BFS]

[POJ]P3126 Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35230   Accepted: 18966 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

CD0J/POJ 851/3126 方老师与素数/Prime Path BFS

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9982   Accepted: 5724 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-digi

HDU 1973 Prime path(BFS+素数表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 题目大意:给定两个四位素数a  b,要求把a变换到b变换的过程要保证  每次变换出来的数都是一个 四位素数,而且当前这步的变换所得的素数 与前一步得到的素数  只能有一个位不同,而且每步得到的素数都不能重复.求从a到b最少需要的变换次数.无法变换则输出Impossible. 如下面的样例:1033 8179 1033 1733 3733 3739 3779 8779 8179 所以答案为6.

POJ 3126 Prime Path bfs 简单

给出2个四位数的素数a,b,要求a每次变化只可以变a的4个数字的其中一个,并且变化后的数也要是素数,问a至少要变化多少次才可以变为b. 注意,a的千位数不能变化为0 先打出素数表,再bfs数a即可. 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 5 using namespace std; 6 7 const int maxn=10010; 8 const int inf=0x3f3f3f3f; 9

Prime Path (BFS)

题目链接:http://poj.org/problem?id=3126 题目大意:给你一个四位数N 和 一个四位数M ,从 N -> M 每次只可以改变一个四位中的任何一位数,但是改变后的数必须是素数,问你最少可以经过多少步使 N 变成 M 思路: 判断素数的话直接利用埃筛就可以了,因为只能改变一位数所以我们每次去找和当前这个数只有一位数不同的素数就可以了! 具体的代码: 1 #include <iostream> 2 #include <string> 3 #include

POJ3126 Prime Path bfs, 水题 难度:0

题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数. 思路 质数明显需要先处理出来,然后采用bfs获取结果即可. 感想 下次需要先计算空间复杂度, 1e8的空间复

POJ 3126 Prime Path (BFS+剪枝)

题目链接:传送门 题意: 给定两个四位数a,b,每次可以改变a的任意一位,并且确保改变后的a是一个素数. 问最少经过多少次改变a可以变成b. 分析: BFS,每次枚举改变的数,有一个剪枝,就是如果这个改变的数之前已经得到过了, 就没有必要继续变回它了. 代码如下: #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <queue&g