poj 3087 bfs

链接:戳这里

Prime Path

Time Limit: 1000MS
Memory Limit: 65536K

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

题意:

两个1000~9999的素数x,y,要求在改变千、百、十、个位上的数字得到一个新的素数

问最少经过几次变换可以使得x->y 注意全程的数都必须是素数

思路:

处理一下1000~9999的素数,并标记

然后暴力改成新的数再去判断  bfs

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int mu[1000100],prime[1000100],vis[1000100],a[1000100];
struct node{
    int v,step;
    node(int v=0,int step=0):v(v),step(step){}
};
int used[1000100];
void BFS(int x,int y){
    mst(used,0);
    queue<node> qu;
    qu.push(node(x,0));
    used[x]=1;
    while(!qu.empty()){
        node now=qu.front();
        qu.pop();
        if(now.v==y){
            cout<<now.step<<endl;
            return ;
        }
        for(int i=0;i<4;i++){
            for(int j=0;j<=9;j++){
                if(i==0 && j==0) continue;
                if(i==0){
                    int tmp=now.v%1000+j*1000;
                    ///printf("%d %d %d %d\n",i,j,now.v,tmp);
                    if(!used[tmp] && vis[tmp]){
                        used[tmp]=1;
                        qu.push(node(tmp,now.step+1));
                    }
                } else if(i==1){
                    int tmp=now.v%100+j*100+(now.v/1000)*1000;
                    if(!used[tmp] && vis[tmp]){
                        used[tmp]=1;
                        qu.push(node(tmp,now.step+1));
                    }
                } else if(i==2){
                    int tmp=now.v/100*100+j*10+now.v%10;
                    if(!used[tmp] && vis[tmp]){
                        used[tmp]=1;
                        qu.push(node(tmp,now.step+1));
                    }
                } else {
                    int tmp=now.v/10*10+j;
                    if(!used[tmp] && vis[tmp]){
                        used[tmp]=1;
                        qu.push(node(tmp,now.step+1));
                    }
                }
            }
        }
    }
}
int main(){
    mu[1]=1;
    int cnt=0,num=0;
    for(int i=2;i<=100000;i++){
        if(!vis[i]){
            prime[cnt++]=i;
            mu[i]=-1;
            if(i>=1000 && i<10000) a[num++]=i;
        }
        for(int j=0;j<cnt;j++){
            if(i*prime[j]>100000) break;
            vis[i*prime[j]]=1;
            if(i%prime[j]==0){
                mu[i*prime[j]]=0;
                break;
            } else mu[i*prime[j]]=-mu[i];
        }
    }
    mst(vis,0);
    for(int i=0;i<num;i++) vis[a[i]]=1;
    int T;
    scanf("%d",&T);
    while(T--){
        int x,y;
        scanf("%d%d",&x,&y);
        BFS(x,y);
    }
    return 0;
}
时间: 2024-09-08 22:41:14

poj 3087 bfs的相关文章

poj 3087 Shuffle&#39;m Up (bfs)

Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several

Poj 3087 Shuffle&#39;m Up 【BFS】

Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6556 Accepted: 3077 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of pok

POJ 3087 Shuffle&#39;m Up (模拟)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5850   Accepted: 2744 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of

poj 3087 Shuffle&#39;m Up (模拟搜索)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5953   Accepted: 2796 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of

POJ 3087 Shuffle&#39;m Up(模拟)

Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

POJ 3087 Shuffle&#39;m Up(模拟退火)

Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several

POJ 3087 Shuffle&#39;m Up (模拟+map)

题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s1,最顶的c块牌归为s2,依此循环下去. 现在输入s1和s2的初始状态 以及 预想的最终状态s12.问s1 s2经过多少次洗牌之后,最终能达到状态s12,若永远不可能相同,则输出"-1". 解题思路:照着模拟就好了,只是判断是否永远不能达到状态s12需要用map,定义map<

[暴力搜索] POJ 3087 Shuffle&#39;m Up

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks o