Prime Path

题目链接Prime Path

题目大意

给定两个四位素数,让你求从第一个数字转化到第二个数字最少需要几步。规则是每次只允许你改变以为数字且改变后的新四位数也必须是素数,不能输出Impossible。

思路

这道题用BFS做,只是判断条件改了一下,但是知道我的代码为什么WA,看到有人先打表,其实不打表我感觉也能过,先贴下代码,希望有小天使帮忙看看为什么。
问题找出来了,原因是再判断素数的函数里我少判断了个等号。。。

这里写图片描述

#include <iostream>
#include <string.h>
#include <queue>
using namespace std;

struct node{
    int num;
    int step;
}st,et;
bool vis[10000];
int x, y;
queue<node> Q;

bool prime(int tt){
    for(int i = 2; i*i <= tt; i++){   //之前写的是i*i<tt
        if(tt % i == 0){
            return false;
        }
    }
    return true;
}

int change(int i, node now){
    int temp = now.num;
    int tt;
    if(i == 0){
        for(int p = 1; p <= 9; p++){
            tt = p*1000 + temp%1000;
            if(prime(tt) && !vis[tt] && tt != temp){
                vis[tt] = true;
                node next;
                next.num = tt;
                next.step = now.step+1;
                Q.push(next);
            }
        }
    }
    else{
        for(int p = 0; p <= 9; p++){
            if(i == 1){
                tt = temp/1000*1000 + p*100 + temp%100;
                if(prime(tt) && !vis[tt] && tt != temp){
                    vis[tt] = true;
                    node next;
                    next.num = tt;
                    next.step = now.step+1;
                    Q.push(next);
                }
            }
            else if(i == 2){
                tt = temp/100*100 + p*10 + temp%10;
                if(prime(tt) && !vis[tt] && tt != temp){
                    vis[tt] = true;
                    node next;
                    next.num = tt;
                    next.step = now.step+1;
                    Q.push(next);
                }
            }
            else if(i == 3){
                tt = temp/10*10 + p;
                if(prime(tt) && !vis[tt] && tt != temp){
                    vis[tt] = true;
                    node next;
                    next.num = tt;
                    next.step = now.step+1;
                    Q.push(next);
                }
            }
        }
    }
}


void bfs(){
    while(!Q.empty()){
        Q.pop();
    }

    Q.push(st);
    vis[x] = true;
    while(!Q.empty()){
        node temp = Q.front();
        Q.pop();
        if(temp.num == y){
            cout << temp.step<< endl;
            return;
        }
        for(int i = 0; i < 4; i++){
            change(i, temp);
        }
    }
   cout << "Impossible" << endl;
    return;
}

int main(){
    ios::sync_with_stdio(false);
    freopen("ftest.txt", "r", stdin);

    int n;
    cin >> n;
    while(n--){
        cin >> x >> y;
        st.num = x;
        st.step = 0;
        et.num = y;
        et.step = 0;
         memset(vis, false, sizeof(vis));
        bfs();
    }
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值