POJ3126Prime Path(BFS)

#include"cstdio"
#include"queue"
#include"cstring"
using namespace std;
const int MAXN=10000;
typedef pair<int,int> P;
int vis[MAXN];
int s,e;
bool prime(int x)
{
    for(int i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            return false;
        }
    }
    return true;
}

void bfs()
{
    queue<P> que;
    que.push(P(0,s));
    vis[s]=1;
    while(!que.empty())
    {
        P now = que.front();que.pop();
        if(now.second==e)
        {
            printf("%d\n",now.first);
            return ;
        }

        int p=now.second%10;
        int pp=(now.second%100)/10;
        for(int i=1;i<=9;i+=2)    //枚举个位
        {
            int next=(now.second/10)*10+i;
            if(!vis[next]&&next!=now.second&&prime(next))
            {
                vis[next]=1;
                que.push(P(now.first+1,next));
            }
        }

        for(int i=0;i<=9;i++)    //枚举十位
        {
            int next=(now.second/100)*100+i*10+p;
            if(!vis[next]&&next!=now.second&&prime(next))
            {
                vis[next]=1;
                que.push(P(now.first+1,next));
            }

        }

        for(int i=0;i<=9;i++)    //枚举百位
        {
            int next=(now.second/1000)*1000+i*100+pp*10+p;
            if(!vis[next]&&next!=now.second&&prime(next))
            {
                vis[next]=1;
                que.push(P(now.first+1,next));
            }
        }

        for(int i=1;i<=9;i++)    //枚举千位
        {
            int next=i*1000+now.second%1000;
            if(!vis[next]&&next!=now.second&&prime(next))
            {
                vis[next]=1;
                que.push(P(now.first+1,next));
            }
        }
    }
    printf("Impossible\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&s,&e);
        memset(vis,0,sizeof(vis));
        bfs();
    }
    return 0;
} 
时间: 2024-12-26 19:48:58

POJ3126Prime 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

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

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(

poj3126--Prime Path(广搜)

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

HDU 6223 Infinite Fraction Path(BFS+剪枝)

The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an audience with the king. He recounted how he had built a happy path in the kingdom of happiness. The king affirmed Welly's talent and hoped

codeforces 1072D Minimum path bfs+剪枝 好题

题目传送门 题目大意: 给出一幅n*n的字符,从1,1位置走到n,n,会得到一个字符串,你有k次机会改变某一个字符(变成a),求字典序最小的路径. 题解: (先吐槽一句,cf 标签是dfs题????) 这道题又学到了,首先会发现,从原点出发,走x步,所有的情况都是在一条斜线上的,而再走一步就是下一条斜线.所以用两个队列进行bfs(把当前步和下一步要处理的字符分开). 到了这里思路就明朗了,每次走的时候如果本身的map里是a就直接走,不是a就看k是否大于0,再看这个字符是不是比答案串里对应位置的字

[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

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.

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的空间复