【简单搜索】Prime Path

Prime Path

问题描述

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.

Input

One line with a positive number: the number of test cases (at most
100). Then for each test case, one line with two numbers separated by
a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost
or containing the word Impossible.

Sample Input

3
1033 8179
1373 8017
1033 1033

Sample Output

6
7
0

简单的bfs+素数筛法。
这里使用的是埃式筛法来构建素数表isprime

  • isprime[i]=true, i为素数
  • isprime[i]=false, i不是素数

然后分个位、十位、百位、千位入口,个位可以排除偶数(包括0),千位的变化范围是[1,9](不能取0)。
值得注意的是,这里没有使用stl中的queue容器,而是用数组自定义了一个线性队列。
其原因引用dalao原话:

你如果不了解STL的内部实现逻辑或者没看过代码,还是少用,因为使用不当就会拖慢程序效率的。STL的queue是在deque基础上封装的,而deque维护的是一个双端队列。换而言之,queue的数据结构决定了,其入队和出队操作的时间复杂度都是0(n),而不是你以为的O(1)。随着队列元素的增长,queue的出入队效率越差。

代码样例

//F-Prime Path
#include<iostream>
#include<cstring>
using namespace std;
#define MAXN 10005
int a,b;
bool isprime[MAXN];
bool vist[MAXN];
struct node{
	int prime, step;
};
void init(){
	memset(isprime, true, sizeof(isprime));
	for(int i=2; i*i<MAXN; i++){
		if(isprime[i]){
			for(int j=i*i; j<MAXN; j+=i){
				isprime[j]=false;
			}
		}
	}
}
void bfs(){
	int head, tail;
	head=tail=0;
	node Q[MAXN];
	Q[tail].prime=a;
	Q[tail].step=0;
	vist[a]=true;
	tail++;
	while(tail>head){
		node temp=Q[head];
		head++;
		if(temp.prime==b){
			cout<<temp.step<<endl;
			return;
		}
		int ones=temp.prime%10;
		int tens=(temp.prime/10)%10;
		for(int i=1; i<10; i+=2){
			int test_ones=(temp.prime/10)*10+i;
			if(!isprime[test_ones]||vist[test_ones]){
				continue;
			}
			vist[test_ones]=true;
			Q[tail].prime=test_ones;
			Q[tail].step=temp.step+1;
			tail++;
		}
		for(int i=0; i<10; i++){
			int test_tens=(temp.prime/100)*100+10*i+ones;
			if(!isprime[test_tens]||vist[test_tens]){
				continue;
			}
			vist[test_tens]=true;
			Q[tail].prime=test_tens;
			Q[tail].step=temp.step+1;
			tail++;
		}
		for(int i=0; i<10; i++){
			int test_hundreds=(temp.prime/1000)*1000+100*i+10*tens+ones;
			if(!isprime[test_hundreds]||vist[test_hundreds]){
				continue;
			}
			vist[test_hundreds]=true;
			Q[tail].prime=test_hundreds;
			Q[tail].step=temp.step+1;
			tail++; 
		}
		for(int i=1; i<10; i++){
			int test_thousands=temp.prime%1000+1000*i;
			if(!isprime[test_thousands]||vist[test_thousands]){
				continue;
			}
			vist[test_thousands]=true;
			Q[tail].prime=test_thousands;
			Q[tail].step=temp.step+1;
			tail++; 
		}
	}
	cout<<"Imopssible"<<endl;
	return;
}
int main(void){
	init();
	int n;
	cin>>n;
	for(int i=0; i<n; i++){
		cin>>a>>b;
		memset(vist, false, sizeof(vist));
		bfs();
	}
	return 0;
}

代码参考至http://exp-blog.com/2018/06/23/pid-818/
是个真大佬。

  • 1605集训计划第6天任务
  • 按时完成
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值