CDOJ 851 方老师与素数 (BFS

题目描述

方老师最近很喜欢素数,他想玩一个游戏:

现在有两个4位的素数n和m,你一次可以改变n的一位数字,并且改变过后的新数字必须也是个素数,并且也不能有前导0。请问使n变为m最少需要多少步。

例如n=1033 m=8179
那么可行的变化是:

1033
1733
3733
3739
3779
8779
8179

输入

第一行有一个整数T(T≤100),代表测试数据的组数。

对于每组数据,每行有两个4位素数N,M(没有前导0)

输出

对于每一组数据,如果能够得到m,输出最少的步数,否则输出Impossible

样例

Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0

题意

BFS 不过这个地方搜索的范围变成了数字 原理还是一样的,一层一层的搜索
对拍了一下,代码应该是对的,,

AC代码

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;

#define ls              st<<1
#define rs              st<<1|1
#define fst             first
#define snd             second
#define MP              make_pair
#define PB              push_back
#define LL              long long
#define PII             pair<int,int>
#define VI              vector<int>
#define CLR(a,b)        memset(a, (b), sizeof(a))
#define ALL(x)          x.begin(),x.end()
#define rep(i,s,e) for(int i=(s); i<=(e); i++)
#define tep(i,s,e) for(int i=(s); i>=(e); i--)

const int INF = 0x3f3f3f3f;
const int MAXN = 10000+10;
const int mod = 1e9+7;
const double eps = 1e-8;

void fe() {
  #ifndef ONLINE_JUDGE
      freopen("in.txt", "r", stdin);
      freopen("out.txt","w",stdout);
  #endif
}
LL read()
{
   LL x=0,f=1;char ch=getchar();
   while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
   while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
   return x*f;
}
int n, m, cnt = 0;
int pr[MAXN];
int prr[MAXN];
bool vis[MAXN*10];
struct node
{
    int x, st;
    node(int _x=0, int _st=0):x(_x),st(_st){}
};
void get_prime() {
    memset(pr, 0, sizeof(pr));
    for (int i = 2; i <= MAXN; i++)
    {
        if (!pr[i])
        {
            pr[++pr[0]] = i;
        }
        for (int j = 1; j <= pr[0] && pr[j] <= MAXN / i; j++)
        {
            pr[pr[j] * i] = 1;
            if (i % pr[j] == 0)
            {
                break;
            }
        }
    }
}
bool check(int x, int y) {
    int k = 0;
    while(x) {
        if(x%10!=y%10) 
            k++;
        x/=10;
        y/=10;
    }
    if(k == 1) 
        return true;
    return false;
}
void bfs() {
    vis[n] = true;
    queue<node> que;
    que.push(node(n,0));
    while(!que.empty()) {
        node tmp = que.front();
        que.pop();
        if(tmp.x == m) {
            cout << tmp.st << endl;
            return ; 
        }
        for(int i = 0; i < cnt; i++) {
            if(!vis[prr[i]] && check(tmp.x,prr[i])) {
                vis[prr[i]] = true;
                que.push(node(prr[i],tmp.st+1));
                //cout << pr[i] << endl;
            }
        }
    }
    cout << "Impossiblie\n";
}
int main(int argc, char const *argv[])
{
    int T;
    cin >> T;
    get_prime();
    for(int i = 1; i < pr[0]; i++) {
        if(pr[i] > 1000) 
            prr[cnt++] = pr[i];
    }
    while(T--) {
        CLR(vis,false);
        cin >> n >> m;
        bfs();
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/wang2332/article/details/80587689
BFS