POJ - 3126 - Prime Path(BFS)

Prime Path

POJ - 3126

题意:

给出两个四位素数 a , b。然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止。要求 c 必须是素数。求变换次数的最小值。(a,b,c都是四位数字,输入时没有前导零)

分析:

  • 每次改变可以获得一个四位数c,然后如果c是素数并且之前没有出现过,那么我们把它放入队列即可。
int f[10001];
int v[10001];
void init()//素数筛
{
    memset(f,0,sizeof f);
    for(int i=2;i<=10000;i++)
    {
        if(f[i]==0)
        {
            for(int j = i+i;j<=10000;j+=i)
            {
                f[j] = 1;
            }
        }
    }
}
int a,b;
int bfs()
{
    memset(v,0,sizeof v);
    queue<int> q;
    q.push(a);
    v[a] = 1;
    int tmp;
    while(!q.empty())
    {
        tmp = q.front();q.pop();
        if(tmp==b)return v[tmp];
        int now;
        int t = v[tmp]+1;
        for(int i=0;i<=9;i++)//循环改变的数字 0 ~ 9
        {
            now = tmp - tmp%10+i;//改变个位
            if(now>=1000&&f[now]==0&&v[now] == 0){v[now] = t;q.push(now);}
            now = tmp/100*100+i*10+tmp%10;//改变十位
            if(now>=1000&&f[now]==0&&v[now] == 0){v[now] = t;q.push(now);}
            now = tmp/1000*1000+i*100+tmp%100;//改变百位
            if(now>=1000&&f[now]==0&&v[now] == 0){v[now] = t;q.push(now);}
            now = i*1000+tmp%1000;//改变千位
            if(now>=1000&&f[now]==0&&v[now] == 0){v[now] = t;q.push(now);}
        }
    }
}
int main()
{
    init();
    int T;cin>>T;
    while(T--)
    {
        cin>>a>>b;
        int ans = bfs();
        cout<<ans-1<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/1625--H/p/9775384.html

时间: 2024-07-28 12:34:03

POJ - 3126 - Prime Path(BFS)的相关文章

POJ 3126 Prime Path(BFS)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12060   Accepted: 6843 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+欧拉线性素数筛)

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

POJ 3126 Prime Path (BFS)

[题目链接]click here~~ [题目大意]给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数 [解题思路]和poj 3278类似.bfs+queue,分别枚举个十百千的每一位就能够了,只是注意个位仅仅能为奇数,且千位从1開始 代码: #ifndef _GLIBCXX_NO_ASSERT #include <cassert> #endif #include <cctype> #include <cerrno&g

POJ 3126 Prime Path(素数路径)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

(简单) POJ 3126 Prime Path,BFS。

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

poj 3126 Prime Path(搜索专题)

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

POJ 3216 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】

<题目链接> 题目大意: 给你两个四位数,它们均为素数,以第一个四位数作为起点,每次能够变换该四位数的任意一位,变换后的四位数也必须是素数,问你是否能够通过变换使得第一个四位数变成第二个四位数. 解题分析: 先打一张素数表,然后进行BFS搜索,对于每次搜索的当前数,枚举某一位与它不同的所有数,判断它是否符合条件,如果符合,就将它加入队列,继续搜索. #include<stdio.h> #include<string.h> #include<iostream>

poj 3126 Prime Path 【暴力BFS】

题意:给你一个4位数,再给你一个4位数,如前一个数的每次只移动一位,问你能不能将第一个数变成第二个. 转移条件:1,只能通过素数作为中转,2,每次移动一位. 如果找到输出最少的转移次数(或步数), 如果找不到输出Impossible. 策略:如题. 直接上代码: #include<stdio.h> #include<string.h> #include<queue> #define M 10005 using std::queue; int vis[10000]; in