POJ3126 Prime Path bfs, 水题 难度:0

题目

http://poj.org/problem?id=3126

题意

多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数。

思路

质数明显需要先处理出来,然后采用bfs获取结果即可。

感想

下次需要先计算空间复杂度, 1e8的空间复杂度肯定会MLE

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 10000;

bool p[maxn];
int ans[maxn];
int b[9 * 4], num;

void calPrime()
{
    p[2] = true;
    for(int i = 3; i < maxn; i += 2)p[i] = true;
    for(int i = 3; i < maxn; i += 2)
    {
        if(p[i])
        {
            for(int j = 3; j * i < maxn; j+= 2)
            {
                p[i * j] = false;
            }
        }
    }
}

void getSameShape(int s)
{
    num = 0;
    for(int base = 1; base < maxn; base *= 10)
    {
        int t = s - ((s % (base * 10)) / base) * base;
        for(int i = 0; i < 10; i++)
        {
            int tmp = t + i * base;
            if(tmp > 1000 && p[tmp] && tmp != s)
            {
                b[num++] = tmp;
            }
        }
    }
}

void calAns(int s, int e)
{
    memset(ans, -1, sizeof ans);
    if(!p[s])return;
    ans[s] = 0;
    queue<int> que;
    que.push(s);
    while(!que.empty())
    {
        s = que.front();
        que.pop();
        getSameShape(s);
        for(int j = 0; j < num; j++)
        {
            if(ans[b[j]] == -1)
            {
                ans[b[j]] = ans[s] + 1;
                if(e == b[j])return;
                que.push(b[j]);
            }
        }

    }
}

int main()
{
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif // LOCAL
    int n, m;
    int T;
    calPrime();
    scanf("%d",&T);
    for(int ti = 0; ti < T && scanf("%d%d", &n, &m) == 2; ti++)
    {
        calAns(n, m);
        if(ans[m] != -1)printf("%d\n",ans[m]);
        else puts("Impossible");
    }
    return 0;
}
时间: 2024-10-19 21:37:27

POJ3126 Prime Path bfs, 水题 难度:0的相关文章

poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697   Accepted: 10800 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio

POJ 1936 All in All 匹配, 水题 难度:0

题目 http://poj.org/problem?id=1936 题意 多组数据,每组数据有两个字符串A,B,求A是否是B的子串.(注意是子串,也就是不必在B中连续) 思路 设置计数器cnt为当前已匹配A的长度,明显在扫描B的过程中只需要记住cnt这一个状态. 扫描B,每次与A[cnt]匹配就将计数器增加1,cnt与A的长度一致时A就是B的子串. 感想 这道题也许可以用更复杂的方法. 代码 1 #include <cstdio> 2 #include <cstring> 3 #i

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

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

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

poj3126(Prime Path)广搜+素数判定

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 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13813   Accepted: 7796 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

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(BFS 数字处理)

题意  给你两个4位素数a, b  你每次可以改变a的一位数但要求改变后仍为素数  求a至少改变多少次才能变成b 基础的bfs  注意数的处理就行了  出队一个数  然后入队所有可以由这个素数经过一次改变而来的素数  知道得到b #include <cstdio> #include <cstring> using namespace std; const int N = 10000; int p[N], v[N], d[N], q[N], a, b; void initPrime(