poj3126 Prime Path bfs 广搜入门

Prime Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 25156 Accepted: 13862

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

题意:给出两个素数n,m。每次只能变化一位数字且每次变化后的数字也必须为素数,求变化的最小次数


其实是一个广搜的题,对每一位数字,如果有:

            1.符合数据范围(1000<=x<10000)(四位数)

            2.为素数

            3.由上一个数字变化任意一位数得到

那么就可以把这个数列入下一个广搜的范围内

找到之后,输出即可

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#define For(i,n) for(int i=0;i<n;i++)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef struct {
    int n,step;
    } Prime;
queue <Prime> a;
const int maxn = 1e4+5;
int prime[maxn];
int Used[maxn];
int m,n;
int ans;

const int Move[4] = {1,10,100,1000};    ///依次判断每一位数
const int Move1[2]= {1,-1};   ///判断加还是减
bool Juge(int x) {          ///判断数据是否合法
    return x>=1000&&x<10000&&!prime[x]&&!Used[x];
    }


bool Jufe(int x,int t){     ///判断是否只改变了一个数字
    int flag = 0;
    while(x&&t){
        if(x%10==t%10)
            flag++;
        x/=10;
        t/=10;
    }
    return flag == 3;
}
void bfs(int x,int step) {
    if(!a.empty())
        a.pop();
    if(x == m) {
        ans = step;
        return;
        }
    For(k,2)
    For(j,4)
    For(i,10){          ///从0-9对每一位进行遍历
        int buf = x+i*Move[j]*Move1[k]; ///下一个数字,3个循环可以考虑到每一种变化
        if(Jufe(buf,x))
        if(Juge(buf)){    ///如果只改变龙一个数字且改变后数据合法
            Prime T;
            T.step = step+1;
            T.n = buf;  
            a.push(T);      ///那么符合条件的数入队 
            Used[buf] = x;  ///同时将该数字标记避免被重复搜索
        }
    }
    if(!a.empty())
        bfs(a.front().n,a.front().step);    ///搜索下一个
}
int main() {
    int t;
    for(int i=2; i*i<maxn; i++)     ///素数打表
        if(!prime[i])
            for(int j = i*i; j<maxn; j+=i)
                prime[j] = 1;
    cin >> t;
    while(t--) {
        mem(Used,0);
        while(!a.empty())
            a.pop();        ///70-72初始化数据
        cin >> n >> m;
        bfs(n,0);
        cout << ans << endl;
        }
    return 0;
    }

posted @ 2018-03-28 17:04  秃头大师  阅读(119)  评论(0编辑  收藏  举报