题目链接 https://vjudge.net/problem/POJ-3126#author=0
萌新第一次写博客,若有错误请见谅,可在评论区指出,后期会改。
poj - 3126 Prime Path(bfs)
poj - 3126 Prime Path(bfs)
中文描述:

poj - 3126 Prime Path(bfs)
此题可以看出为广度搜索,因为求解最少步数,所以4位数从最小开始,基于每一位一层层遍历,同时使用队列,符合条件入队,作为下一步bfs的数据。具体解释见注释。。。。。。。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 10000
using namespace std;

int vis[maxn];//记录步数的数组。

bool Prim(int x)//判断是否为素数。
{
 if(x<1000)
  return false;//防止出现前导零。
 for(int i = 2;i<=sqrt(x);i++)
 {
  if(x%i==0)
   return false;
 }
 return true;
}

int turn(int x,int i)//转换函数,将四位中的一位转为零。
{
    char s[20]={0};
    sprintf(s,"%d",x);//和sscanf是两个不错的数和字符串互相转化的函数。
    s[i] = '0';
    sscanf(s,"%d",&x);
    return x;
}

int bfs(int x,int y)
{
    queue<int> q;
    while(!q.empty())
        q.pop();
    memset(vis,0,sizeof(vis));//数组清零,很重要~~。
    q.push(x);//输入的数据入队。
    vis[x] = 1;
    while(!q.empty())
    {
        x = q.front();
        q.pop();
        if(x==y)
            return vis[x]-1;
        else
        {
            int q1,q2,t = 1000;
            for(int i = 0;i<4;i++)
            {
                q1 = turn(x,i);//对数字分层遍历;.
                for(int j = 0;j<=9;j++)
                {
                    q2 = q1+t*j;
                    if(Prim(q2)&&vis[q2]==0)
                    {
                        q.push(q2);
                        vis[q2] = vis[x]+1;
                    }
                }
                t/=10;
            }
        }
    }
    return -1;//如果队列数据pop完毕,未找到则为Impossible。
}

int main()
{
 int T,m,n;
 cin>>T;
 for(int i = 0;i<T;i++)
 {
  cin>>m>>n;
  int ans = bfs(m,n);
  if(ans==-1)
            cout<<"Impossible"<<endl;
        else
            cout<<ans<<endl;
  }
  return 0;
}

另外可打素数表进行优化,快大约200多ms…。。。。。


#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 10000
using namespace std;

int vis[maxn];
int p[maxn];

int Prim(int x)
{
 for(int i = 2;i<=sqrt(x);i++)
 {
  if(x%i==0)
   return 0;
 }
 return 1;
}

bool Pan(int x)
{
    if(x<1000||p[x]==0)
        return false;
    return true;
}

int turn(int x,int i)
{
    char s[20]={0};
    sprintf(s,"%d",x);
    s[i] = '0';
    sscanf(s,"%d",&x);
    return x;
}

int bfs(int x,int y)
{
    queue<int> q;
    while(!q.empty())
        q.pop();
    memset(vis,0,sizeof(vis));
    q.push(x);
    vis[x] = 1;
    while(!q.empty())
    {
        x = q.front();
        q.pop();
        if(x==y)
            return vis[x]-1;
        else
        {
            int q1,q2,t = 1000;
            for(int i = 0;i<4;i++)
            {
                q1 = turn(x,i);
                for(int j = 0;j<=9;j++)
                {
                    q2 = q1+t*j;
                    if(Pan(q2)&&vis[q2]==0)
                    {
                        q.push(q2);
                        vis[q2] = vis[x]+1;
                    }
                }
                t/=10;
            }
        }
    }
    return -1;
}

int main()
{
    for(int i = 999;i<=maxn;i++)
    {
        p[i] = Prim(i);
    }
 int T,m,n;
 cin>>T;
 for(int i = 0;i<T;i++)
 {
  cin>>m>>n;
  int ans = bfs(m,n);
  if(ans==-1)
            cout<<"Impossible"<<endl;
        else
            cout<<ans<<endl;
 }
}

poj - 3126 Prime Path(bfs)

相关文章: