POJ 3126 题解

题目地址

这是地址

AC代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
#include <cmath>
#include <cstring>
#include <deque>
#include <queue>
#include <cstdio>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    int list[10001];
    int mark[10001];
    memset(list,0, sizeof(list));
    list[0]=list[1]=1;
    for (int i = 2; i <=10000 ; ++i) {
        if(list[i]==0){
            for (int j = i*2; j <=10000 ; j+=i) {
                list[j]=1;
            }
        }
    }
    int t;
    cin>>t;
    while(t--){
        int a,b;
        cin>>a>>b;
        memset(mark,0, sizeof(mark));
        queue<pair<int,int> >ans;
        ans.push(make_pair(a,0));
        while(1){
            int f,s;
            f=ans.front().first;
            s=ans.front().second;
            ans.pop();
            mark[f]=1;
            if(f==b){
                cout<<s<<endl;
                break;
            }
            int temp;
            for(int i = 1;i<40;i++){
                if(i/10==0){
                    if(i%2==0)continue;
                    temp = f/10;
                    temp*=10;
                    temp+=i;
                    if(temp!=f&&list[temp]==0&&mark[temp]==0)ans.push(make_pair(temp,s+1));
                }else if(i/20==0){
                    int x;
                    temp = f%10;
                    x = f/100;
                    x*=10;
                    x+=(i%10);
                    x*=10;
                    x+=temp;
                    if(x!=f&&list[x]==0&&mark[x]==0)ans.push(make_pair(x,s+1));
                }else if(i/30==0){
                    int x,y;
                    x = f%100;
                    y = x/10;
                    x = x%10;
                    temp = f/1000;
                    temp*=10;
                    temp+=(i%10);
                    temp*=10;
                    temp+=y;
                    temp*=10;
                    temp+=x;
                    if(temp!=f&&list[temp]==0&&mark[temp]==0)ans.push(make_pair(temp,s+1));
                }else if(i/40==0&&i%10!=0){
                    temp = f%1000;
                    temp += (i%10)*1000;
                    if(temp!=f&&list[temp]==0&&mark[temp]==0)ans.push(make_pair(temp,s+1));
                }
            }
        }
    }
    return 0;
}

题解和题目思路

请不要吐槽我乱七八糟的if-else和里面奇奇怪怪的一大堆变量//菜还是你菜
因为后面经常要判断素数所以这道题先打一个线性筛为敬,到时候剪枝查是不是素数就快很多了,至于为什么要写一个MARK数组,是因为后面在改每一位的数字并且入队时经常性会出现重复修改的情况(刚刚改过去现在又改回来了这样子的),加一个mark就可以很好的解决重复劳动的问题,有效减小你TLE的机会(滑稽)
然后就是大家喜闻乐见的BFS环节,这道题是四位数,每位都有10种情况,合计四十种情况(扶额)改到要亲命了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值