POJ 3126 Prime Path (BFS)

题目链接click here~~

题目大意】给你n,m各自是素数,求由n到m变化的步骤数,规定每一步仅仅能改变个十百千一位的数,且变化得到的每个数也为素数

解题思路】和poj 3278类似。bfs+queue,分别枚举个十百千的每一位就能够了,只是注意个位仅仅能为奇数,且千位从1開始

代码:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

#define rep(i,j,k) for(int i=(int)j;i<(int)k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?

a:b
#define Min(a,b) a>b?

b:a
#define mem(a,b) memset(a,b,sizeof(a))

typedef long long LL;
typedef unsigned long long LLU;
typedef double db;
const int N=15000;
const int inf=0x3f3f3f3f;
int n,m,T;

bool vis[N];

int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
int dir6[6][3]= {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};///六个方向

inline LL read()
{
    int c=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
    return c*f;
}

bool isPrime(int t)///素数推断
{
    for(int i=2; i<=sqrt(t); ++i)
        if(t%i==0) return false;
    return true;
}

struct node
{
    int val,step;
};

void bfs()
{
    mem(vis,false);
    node a,b;
    a.val=n;
    a.step=0;
    queue<node >vall;
    vall.push(a);
    while(!vall.empty()){
        a=vall.front();
        vall.pop();
        if(a.val==m){
            printf("%d\n",a.step);
            return ;
        }
        int ge=a.val%10;///枚举个位
        int shi=a.val%100/10;///枚举十位

        for(int i=1; i<=9; i+=2){///个位
            int y=(a.val/10)*10+i;
            if(y!=a.val&&isPrime(y)&&!vis[y]){
                b.val=y;
                b.step=a.step+1;
                vis[y]=1;
                vall.push(b);
            }
        }

        for(int i=0; i<=9; i++){///十位
            int y=(a.val/100)*100+i*10+ge;
            if(y!=a.val&&isPrime(y)&&!vis[y]){
                b.val=y;
                b.step=a.step+1;
                vis[y]=1;
                vall.push(b);
            }
        }

        for(int i=0; i<=9; i++){///百位
            int y=(a.val/1000)*1000+i*100+ge+shi*10;
            if(y!=a.val&&isPrime(y)&&!vis[y]){
                b.val=y;
                b.step=a.step+1;
                vis[y]=1;
                vall.push(b);
            }
        }

        for(int i=1; i<=9; i++){///千位
            int y=a.val%1000+i*1000;
            if(y!=a.val&&isPrime(y)&&!vis[y]){
                b.val=y;
                b.step=a.step+1;
                vis[y]=1;
                vall.push(b);
            }
        }
    }

    puts("Impossible");
    return ;
}
int main()
{
    T=read();
    while(T--)
    {
        n=read(),m=read();
        if(n==m) puts("0");
        else bfs();
    }
    return 0;
}

/*
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
*/
时间: 2024-10-18 11:35:56

POJ 3126 Prime Path (BFS)的相关文章

POJ 3126 Prime Path(BFS)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12060   Accepted: 6843 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-dig

POJ 3126 Prime Path (bfs+欧拉线性素数筛)

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

POJ - 3126 - Prime Path(BFS)

Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数的最小值.(a,b,c都是四位数字,输入时没有前导零) 分析: 每次改变可以获得一个四位数c,然后如果c是素数并且之前没有出现过,那么我们把它放入队列即可. int f[10001]; int v[10001]; void init()//素数筛 { memset(f,0,sizeof f); fo

POJ 3216 Prime Path (BFS)

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

POJ 3126 Prime Path(素数路径)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

poj 3126 Prime Path(搜索专题)

Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20237   Accepted: 11282 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-di

HDU - 1973 - Prime Path (BFS)

Prime Path Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 987    Accepted Submission(s): 635 Problem Description The ministers of the cabinet were quite upset by the message from the Chief of S

(简单) POJ 3126 Prime Path,BFS。

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

poj3126——Prime Path(BFS)

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