zoukankan      html  css  js  c++  java
  • POJ 3126-Prime Path(BFS)

    题目地址:POJ 3126
    题意:给你两个4位的都是素数正整数n,m,每次能够变 个十百千位 中的一个数(变化后也是素数,问最少经过多少次变化n能变成m,假设不能输出Impossible(窝认为并没有不成立的情况
    思路:每次变化一位,然后搜就好了。

    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <stdlib.h>
    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <set>
    #include <queue>
    #include <stack>
    #include <map>
    #pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef __int64  LL;
    const int inf=0x3f3f3f3f;
    const double pi= acos(-1.0);
    const double esp=1e-7;
    const int Maxn=1e6+10;
    struct node
    {
        int x1,x2,x3,x4;
        int cnt;
    }q[Maxn],f1,f2;
    int n,m;
    int vis[Maxn];
    int isprime(int x)
    {
        for(int i=2;i<=sqrt(x);i++)
            if(x%i==0)
               return 0;
        return 1;
    }
    void BFS()
    {
        memset(vis,0,sizeof(vis));
        queue<node >q;
        f1.x1=n%10,n/=10;
        f1.x2=n%10,n/=10;
        f1.x3=n%10,n/=10;
        f1.x4=n;
        f1.cnt=0;
        vis[n]=1;
        q.push(f1);
        while(!q.empty()){
            f1=q.front();
            q.pop();
            if(f1.x4*1000+f1.x3*100+f1.x2*10+f1.x1==m){
                printf("%d
    ",f1.cnt);
                return ;
            }
            for(int i=1;i<=9;i++){
                if(i!=f1.x4){
                    int tmp=i*1000+f1.x3*100+f1.x2*10+f1.x1;
                    if(isprime(tmp)&&!vis[tmp]){
                        vis[tmp]=1;
                        f2.x4=i;
                        f2.x3=f1.x3;
                        f2.x2=f1.x2;
                        f2.x1=f1.x1;
                        f2.cnt=f1.cnt+1;
                        q.push(f2);
                    }
                }
            }
            for(int i=0;i<=9;i++){
                if(i!=f1.x3){
                    int tmp=f1.x4*1000+i*100+f1.x2*10+f1.x1;
                    if(isprime(tmp)&&!vis[tmp]){
                        vis[tmp]=1;
                        f2.x4=f1.x4;
                        f2.x3=i;
                        f2.x2=f1.x2;
                        f2.x1=f1.x1;
                        f2.cnt=f1.cnt+1;
                        q.push(f2);
                    }
                }
            }
            for(int i=0;i<=9;i++){
                if(i!=f1.x2){
                    int tmp=f1.x4*1000+f1.x3*100+i*10+f1.x1;
                    if(isprime(tmp)&&!vis[tmp]){
                        vis[tmp]=1;
                        f2.x4=f1.x4;
                        f2.x3=f1.x3;
                        f2.x2=i;
                        f2.x1=f1.x1;
                        f2.cnt=f1.cnt+1;
                        q.push(f2);
                    }
                }
            }
            for(int i=0;i<=9;i++){
                if(i!=f1.x3){
                    int tmp=f1.x4*1000+f1.x3*100+f1.x2*10+i;
                    if(isprime(tmp)&&!vis[tmp]){
                        vis[tmp]=1;
                        f2.x4=f1.x4;
                        f2.x3=f1.x3;
                        f2.x2=f1.x2;
                        f2.x1=i;
                        f2.cnt=f1.cnt+1;
                        q.push(f2);
                    }
                }
            }
        }
        puts("Impossible");
        return ;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%d %d",&n,&m);
            BFS();
        }
        return 0;
    }
    
  • 相关阅读:
    问题账户需求分析
    2018年春季个人阅读计划
    图表分析
    《软件需求与分析》需要掌握的内容
    假期实践体验
    开发进度12
    开发进度11
    python+opencv实现轮廓形状拟合
    ubuntu无法安装vim、tree等解决办法
    报错 version `GLIBCXX_3.4.22' not found
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7071441.html
Copyright © 2011-2022 走看看