ACW 4220. 质数路径 训练记录day3

我超 好臭的代码 不会连边 随便乱写吧

```

#include <bits/stdc++.h>
using namespace std;
const int N =1e4+10;
const int mod =11451419;
#define endl '\n'
#define Endl '\n'
#define inf 1e18
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
int a[N],x,y;
int h[N],e[N<<1],ne[N<<1],idx,dist[N<<1];
int bfs(){
    memset(dist,-1,sizeof dist);
    dist[x]=0;
    queue<int>q;
    q.push(x);
    while(!q.empty()){
        auto t = q.front();
        q.pop();
        for(int i=h[t];i!=-1;i=ne[i]){
            int j=e[i];
            if(dist[j]==-1){
                dist[j]=dist[t]+1;
                if(j==y)return dist[y];
                q.push(j);
            }
        }
    }
    return dist[y];
}
void yu(){
    for(int i=1000;i<=9999;i++){
        int j;
        for(j=2;j<i;j++){
            if(i%j==0)break;
        }
        if(j==i)a[i]++;
    }
}
void add(int a,int b){
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
void lianbian(){
    memset(h,-1,sizeof h);
    for(int i=1000;i<=9999;i++){
        if(!a[i])continue;
        int cnt=i;
        for(int j=0;j<9;j++){
            if(i%10==9)i-=10;
            i++;
            if(a[i]&&i<=9999)add(cnt,i),add(i,cnt);
        }
        i=cnt;
        for(int j=0;j<9;j++){
            if(i%100-i%10==90)i-=100;
            i+=10;
            if(a[i]&&i<=9999)add(cnt,i),add(i,cnt);
        }
        i=cnt;
        for(int j=0;j<9;j++){
            if(i%1000-i%100==900)i-=1000;
            i+=100;
            if(a[i]&&i<=9999)add(cnt,i),add(i,cnt);
        }
        i=cnt;
        for(int j=0;j<9;j++){
            if(i%10000-i%1000==9000)i-=10000;
            i+=1000;
            if(a[i]&&i<=9999)add(cnt,i),add(i,cnt);
        }
        i=cnt;
    }
}
signed main(){
    fast
    int t;cin>>t;
    yu();
    lianbian();
    while(t--){
        cin>>x>>y;
        int tt=bfs();
        if(tt==-1)cout<<"Impossible"<<Endl;
        else cout<<tt<<endl;
    }
    return 0^0;
}

```

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值