HDU 1973

Prime Path

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 463    Accepted Submission(s): 305

Problem 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

3

1033 8179

1373 8017

1033 1033

Sample Output

6

7

0

//这题是求从第一个数字变为第二个数字最少须要变几次,每一次仅仅能改变一个数字,而且要满足改变之后的仍然是素数

//从第一个字符到第四个字符相当于迷宫中的四个方向 接着每次改变从0-9注意开头第一个字符不能以0开头

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
bool vis[9999];  //标记数组
char start[10];  //起始数字用字符串处理
int stop;  //结尾数字
struct Node
{
    char tem[10];
    int step;
};
int prime(int n)  //推断素数
{
    for(int i=2;i*i<=n;i++)
    {
        if(n%i==0)
            return 0;
    }
    return 1;
}

int bfs(char p[])
{
    queue <Node> q;
    memset(vis,0,sizeof(vis));
    int temp;
    Node a;
    strcpy(a.tem,p);
    a.step=0;
    q.push(a);
    while(!q.empty())
    {
        Node b;
        b=q.front();
        q.pop();
        sscanf(b.tem,"%d",&temp);  //利用sscanf函数将字符串转换为数字直接与结尾比較
        if(temp==stop)
            return b.step;
        vis[temp]=1;
        for(int i=0;i<4;i++)
        {
            char k;
            k=i!=0?'0':'1';
            for(;k<='9';k++)
            {
                Node c;
                c=b;
                c.tem[i]=k;
                sscanf(c.tem,"%d",&temp);
                if(prime(temp)&&!vis[temp])  //约束条件
                {
                    c.step++;
                    q.push(c);
                    vis[temp]=1;
                }
            }
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        scanf("%s%d",start,&stop);
        int flag=bfs(start);
        int flag=bfs(start);
        if(flag==-1)
            printf("Impossible\n");
        else
            printf("%d\n",flag);
    }
    return 0;
}
时间: 2024-11-11 13:58:56

HDU 1973的相关文章

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.

hdu 1973 Prime Path

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path 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

hdu 1973 bfs+素数判断

题意:给出两个四位数,现要改变第一个数中的个,十,百,千位当中的一个数使它最终变成第二个数,要求这过程中形成的数是素数,问最少的步骤题解:素数筛选+bfsSample Input31033 81791373 80171033 1033Sample Output670 注意第一位不能变成0即可 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5

HDU - 1973 - Prime Path (BFS)

Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 987    Accepted Submission(s): 635 Problem Description The ministers of the cabinet were quite upset by the message from the Chief of S

HDU1973 http://acm.hdu.edu.cn/showproblem.php?pid=1973

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<queue> #include<math.h> #define N 10010 using namespace std; int vis[N]; char s1[5], s2[5], s3[5]; struct node { int step; char st[5]; }; int prime(int y) { int

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往