POJ - 3126 Prime Path【bfs】

Prime Path

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 30561 Accepted: 16589
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 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
Source

Northwestern Europe 2006

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

简述:给定两个素数,计算从一个素数变为另一个素数的步数。每一次改变一个数字且改变的数字也要是素数。

分析:首先写一个判断n是否为为素数的函数,是就返回-1,不是就返回-2.
接着声明一个数组,用来储存2-10050是否为素数。数组内容{-1,-1,-2,-1……}
再将该数组全部内容复制到f数组中
x表示第一个四位素数,y用来表示最终目标
将f数组中第x个数置为零
接着运用queue p
q.push(x);将x压入队列末端

循环直到队列为空
{
t放在队列首位,用tmp代为计算,并取出
此时,开始换位数
用一个数组第0-3分别记忆tmp的各位到千位
换个位:可换0-9,如果换完之后是素数,那么f[tmp]=f[t]+1,随即压入队列末端
换十位、百位、千位
最后,如果f[y]不是-1时,即前面换位数时已经搜索到了y时,输出f[y]的值即可
}

说明:运用memcpy()一定要加头文件<string.h>,改变位数时,个位数字只能改成奇数,十位、百位数字0-9,千位数字从1开始。

AC代码如下:

#include <iostream>
#include <cmath>
#include <queue>
#include <string.h>//一定要加.h
using namespace std;
int judge(int n)
{
	int i;
	for (i = 2; i*i <= n; i++)
	{
		if (n % i == 0) return -2;
	}
	return -1;
}
int a[10000], f[10000];
int main()
{
	int n;
	int x, y, tem, t, i, k = 0;
	for (i = 2; i < 10000; i++) a[i] = judge(i);
	cin >> n;
	while (n--)
	{
		
		int jiwei[5] = { 0 };
		cin >> x >> y;
		memcpy(f, a, 10000 * sizeof(int));
		f[x] = 0;
		queue<int>q;
		q.push(x);
		while (!q.empty()&&(f[y]==-1))
		{
			t = q.front();
			tem = t;
			q.pop();
			for (i = 1; i <= 4; i++)
			{
				jiwei[i] = tem % 10;
				tem /= 10;
			}
			for (i = 1; i <= 9; i += 2)
			{
				tem = i + jiwei[2] * 10 + jiwei[3] * 100 + jiwei[4] * 1000;
				if (f[tem] == -1)
				{
					f[tem] = f[t] + 1;
					q.push(tem);
				}
			}
			for (i = 0; i <= 9; i ++)
			{
				tem = jiwei[1] + i * 10 + jiwei[3] * 100 + jiwei[4] * 1000;
				if (f[tem] == -1)
				{
					f[tem] = f[t] + 1;
					q.push(tem);
				}
			}
			for (i = 0; i <= 9; i++)
			{
				tem = jiwei[1] + jiwei[2] * 10 + i * 100 + jiwei[4] * 1000;
				if (f[tem] == -1)
				{
					f[tem] = f[t] + 1;
					q.push(tem);
				}
			}
			for (i = 1; i <= 9; i++)
			{
				tem = jiwei[1] + jiwei[2] * 10 + jiwei[3] * 100 + i * 1000;
				if (f[tem] == -1)
				{
					f[tem] = f[t] + 1;
					q.push(tem);
				}
			}
			
		}
		if (f[y] != -1) cout << f[y] << endl;
		else cout << "Impossible" << endl;
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值