F - Prime Path

题目大意:

素数路径

估计看数据就明白这道题什么意思了......给两个素数,都是四位数的素数,并且没有前导0,现在需要经过一种变换把一个素数转换成另一个,当然这种转换是有规则的,规则就是每次只能改变这个四位数的其中一位数字,当然改变后的数字也得是素数,问最少的改变次数是多少......

貌似还是广搜..............................................................................................不过做起来应该会麻烦点,要求素数,不过可以搞一个素数表这样判断起来更方便

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;

#define maxn 10000

int p[maxn];//标记4位数的素数

int Prime(int n)
{
    int i, k=sqrt(n);

for(i=2; i<=k; i++)
        if(n % i == 0)
            return 0;

return 1;
}
int Turn(int n, int k)//把n的第k位转换成0
{
    char s[10]={0};

sprintf(s, "%d", n);
    s[k] = ‘0‘;
    sscanf(s, "%d", &n);

return n;
}
int BFS(int s, int e)
{
    int i, j, k, q;
    int v[maxn]={0};
    queue<int> Q;

Q.push(s);
    v[s] = 1;

while(Q.size())
    {
        s = Q.front();Q.pop();

if(s == e)
            return v[s]-1;

int t = 1000;

for(i=0; i<4; i++)
        {
            q = Turn(s, i);

for(k=0; k<10; k++)
            {
                j = q+k*t;

if(p[j] == 1 && v[j] == 0)
                {
                    Q.push(j);
                    v[j] = v[s] + 1;
                }
            }

t /= 10;
        }
    }

return -1;
}

int main()
{
    int i, s, e, T;

for(i=1000; i<maxn; i++)
        p[i] = Prime(i);

scanf("%d", &T);

while(T--)
    {
        scanf("%d%d", &s, &e);

int ans = BFS(s, e);

if(ans == -1)
            printf("Impossible\n");
        else
            printf("%d\n", ans);
    }

return 0;

}

时间: 2024-08-29 02:05:25

F - Prime Path的相关文章

F - Prime Path POJ 3126 筛选素数+bfs

F - Prime Path Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3126 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have t

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

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); fo

(简单) 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

POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted: 9153 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 th

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

Prime Path (poj 3126 bfs)

Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 6640 Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to c

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