Prime Path(bfs)广度优先搜索

news/2024/7/7 19:16:54

题目描述

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 keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.

Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
实际上就是给你两个数(都是素数),每一次可以变化第一个数的某一位(变化后也需要是素数),问最少变化几次可以变成第二个数。这里用一波bfs。

至于为什么用广搜,是因为他需要求最少的变化次数,所以搜索方法运用了广搜

输入

3
1033 8179
1373 8017
1033 1033

输出

6
7
0

贴一波代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <math.h>
#include <string>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#define maxn  1000000
const int MaxN=0x3f3f3f3f;
const int MinN=0xc0c0c00c;
typedef long long ll;
using namespace std;
bool isprim[100010];
void prim(){isprim[1]=false;for(int i=2;i<=100010;i++)if(isprim[i])for(int f=2*i;f<=100010;f=f+i)isprim[f]=false;
}
int num[10];
int x,y;
bool visited[100010];
int ans=0;
struct wazxy{int x;int steps;
}node,temp;queue<wazxy>  q;void bfs(){node.x=x,node.steps=0;q.push(node);while(!q.empty()){temp=q.front();q.pop();if(temp.x==y){ans=temp.steps;return ;}else{int ii=0;while(temp.x){num[++ii]=temp.x%10;temp.x/=10;}//            for(int i=1;i<=4;i++){
//                cout<<num[i]<<" ";
//            }
//            cout<<endl;for(int i=1;i<=4;i++){  //分别用来分成改变个位十位百位千位的数字时for(int f=0;f<=9;f++){int now;if(i==1){if(f==num[1])  continue;now=num[4]*1000+num[3]*100+num[2]*10+f;//更改某位的数字,先将他拆开改变之后再合起来。if(isprim[now]&&visited[now]){  //我这个visited用来去重,防止变成某数又在变回去,而且一定要确定他是个素数node.steps=temp.steps+1;node.x=now;visited[now]=false;  //标记q.push(node);}}if(i==2){if(f==num[2])  continue;now=num[4]*1000+num[3]*100+f*10+num[1];if(isprim[now]&&visited[now]){node.steps=temp.steps+1;node.x=now;visited[now]=false;q.push(node);}}if(i==3){if(f==num[3])  continue;now=num[4]*1000+f*100+num[2]*10+num[1];if(isprim[now]&&visited[now]){node.steps=temp.steps+1;node.x=now;visited[now]=false;q.push(node);}}if(i==4){if(f==0||f==num[4])  continue;//注意改变千位数字是不能为0now=f*1000+num[3]*100+num[2]*10+num[1];if(isprim[now]&&visited[now]){node.steps=temp.steps+1;node.x=now;visited[now]=false;q.push(node);}}}}}}
}int main()
{memset(isprim,true,sizeof(isprim));prim();//for(int i=0;i<=100;i++)  if(isprim[i])  cout<<i<<" ";int t;cin>>t;for(int i=0;i<t;i++){memset(visited,true,sizeof(visited));cin>>x>>y;visited[x]=false;while(!q.empty())  q.pop();  //将队列清空!!!!!!!(之前一个题没有清空找了好久)ans=0;bfs();cout<<ans<<endl;}}

http://lihuaxi.xjx100.cn/news/286071.html

相关文章

一些改进模型速度/精度的工程方法

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达作者&#xff1a;Captain Jackhttps://zhuanlan.zhihu.com/p/92025012本文已由作者授权&#xff0c;未经允许&#xff0c;不得二次转载一些自己的工作经验总结&#xff0c;用…

HDU4160(最小路径覆盖问题)

题意&#xff1a;当满足条件wi<wj,hi<hl和li<lj时&#xff0c;求解通过优化嵌套给定的娃娃可以获得的最外层洋娃娃的最小数量。 思路&#xff1a;如果嵌套的娃娃越多&#xff0c;则剩下的娃娃就越少&#xff0c;意味着单独出来的娃娃越少&#xff0c;转换为求解最小路…

线段树分治 ---- CF1217F - Forced Online Queries Problem(假离线 可撤销并查集 + 线段树分治)详解

题目链接 题目大意 解题思路&#xff1a; 我一开始想到可以用可撤销并查集去维护这种删边加边的操作&#xff0c;但是有个缺点是每次撤销都有把后面的边全部撤销复度是O(n2)O(n^2)O(n2) 首先我们考虑这种动态加边删边的问题&#xff0c;如果是离线的话&#xff0c;那就是线段…

秘籍 | 机器学习数据集网址大全

作者 | Will Badr译者 | Linstancy整理 | Jane出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;要找到一定特定的数据集可以解决各种机器学习问题&#xff0c;是一件很难的事情。越来越多企业或研究机构将自己的数据集公开&#xff0c;已经成为全球的趋势&#xff0c;…

第一个Servlet和Jsp

为什么80%的码农都做不了架构师&#xff1f;>>> 第一个Servlet和Jsp 开发Servlet有3种方法1.Servlet接口2.继承GenericServlet3.继承HttpServlet Tomcat 9部署Servlet 1.Tomcat的安装目录的webapps目录&#xff0c;可以看到ROOT&#xff0c;examples, tomcat-docs之…

Dungeon Master(bfs)广度优先搜索

描述 You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonal…

你的代码将会被GitHub埋在北极,保存1000年!

点击上方“视学算法”&#xff0c;选择加"星标"或“置顶”重磅干货&#xff0c;第一时间送达晓查 发自 凹非寺 本文转载自&#xff1a;量子位&#xff08;QbitAI&#xff09;你写的代码将被会被GitHub保存1000年。GitHub是不是疯了&#xff1f;有网友吐槽&#xff1a…

明晚8点公开课 | 用AI给旧时光上色!详解GAN在黑白照片上色中的应用

在改革开放40周年之际&#xff0c;百度联合新华社推出了一个刷屏级的H5应用——用AI技术为黑白老照片上色&#xff0c;浓浓的怀旧风勾起了心底快被遗忘的时光。想了解如何给老照片上色&#xff1f;本次公开课中&#xff0c;我们邀请到了百度高级研发工程师李超&#xff0c;他的…