poj3126——bfs

POJ 1326  对数位的bfs

Prime Path

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12480   Accepted: 7069

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

题意:求从一个四位数素数到另一个四位数素数所需的最短步数,每一步只能改变一位数字,且改变过程中的数必须是四位数这是一道典型的数位搜索题,求最短路所以用bfs,难点在于模拟数的转化,另外注意边界控制

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

const int maxn=10000;

int n,m;
bool vis[maxn];
int ans;
struct node
{
    int num,step;
};

int mypow(int n,int k)
{
    int res=1;
    while(k--) res*=n;
    return res;
}

bool isprime(int n) //判断素数,由于数据不大,懒得打印素数表了
{
    for(int i=2;i*i<=n;i++){
        if(n%i==0) return false;
    }
    return true;
}

int change(int n,int i,int j) //模拟数的转化,有很多技巧方法,一定要细心
{
    int f=n/mypow(10,i+1)*mypow(10,i+1);//保留前缀
    int r=n%mypow(10,i);//保留后缀
    int mid=j*mypow(10,i); //改变中间的数
    return f+mid+r;
}

bool bfs()
{
    memset(vis,0,sizeof(vis));
    queue<node> q;
    q.push({n,0});
    vis[n]=1;
    while(!q.empty()){
        node now=q.front();
        q.pop();
        if(now.num==m){
            ans=now.step;
            return true;
        }
        for(int i=0;i<4;i++){  //从第1位到第四位
            for(int j=0;j<=9;j++){  //把所在位数字改为j
                int nextnum=change(now.num,i,j);
                if(nextnum<1000||nextnum>=maxn) continue;//边界控制
                if(vis[nextnum]||!isprime(nextnum)) continue;
                vis[nextnum]=1;
                q.push({nextnum,now.step+1});
            }
        }
    }
    return false;
}

int main()
{
    int T;cin>>T;
    while(T--){
        cin>>n>>m;
        if(bfs()) cout<<ans<<endl;
        else cout<<"Impossible"<<endl;
    }
    return 0;
}

poj3126_bfs

时间: 2024-10-05 14:59:42

poj3126——bfs的相关文章

poj3126(bfs)

题目链接:http://poj.org/problem?id=3126 题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后的数都是素数. 分析:筛选素数+bfs,枚举每一位数字进行修改,修改后还是素数的进入队列,循环出队入队,最后改变一步.两步.三步...变成的素数散落开来,如同一棵树,遇到与要改变目标相同的数时返回步数即可. #include <cstdio> #include <cstring> #i

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

poj3126 筛素数+bfs

1 //Accepted 212 KB 16 ms 2 //筛素数+bfs 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <queue> 7 using namespace std; 8 const int inf = 100000000; 9 const int imax_n = 10005; 10 bool pri[imax_n]; 11 bool vi

POJ3126 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

poj3126——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

poj3126

被坑了3个小时,本来以为算法错了,谁知道,竟然是素数筛弄错了 !!! #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>using namespace std;int a[10001];int v[10001];int n,m;struct node{ int ans,x;}q[100001];void bfs()

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};

BFS+康托展开(洛谷1379 八数码难题)

在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出一种初始布局(初始状态)和目标布局(为了使题目简单,设目标状态为123804765),找到一种最少步骤的移动方法,实现从初始布局到目标布局的转变. 输入格式: 输入初试状态,一行九个数字,空格用0表示 输出格式: 只有一行,该行只有一个数字,表示从初始状态到目标状态需要的最少移动次数(测试数据中无特殊无法到达目标状态数据) 输入样例#1: 2831

Sicily 1444: Prime Path(BFS)

题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 1 #include<bits/stdc++.h> 2 using namespace std; 3 4 bool isPrime(int n){//素数判断 5 if(n == 2 || n == 3) return true; 6 else{ 7 int k = sqrt(n) + 1; 8 for(int i = 2; i < k; i