寒假训练补题-第三天-H - 8-BFS(较易)

原题链接:http://poj.org/problem?id=3126
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.

**内阁部长对安全主任的信息感到非常不安,他们说他们都必须改变办公室的四位数房间号码。

  • 不时地改变这些事情是为了安全,以便让敌人陷入黑暗。
  • 但是看,我选择了我的号码1033是有充分理由的。
    你知道,我是总理!
  • 我知道,所以你的新号码8179也是一个素数。
    您只需在办公室门上的四个旧数字上粘贴四个新数字即可。
  • 不,这不是那么简单。
    假设我将第一个数字更改为8,那么数字将为8033,这不是素数!
  • 我知道,作为总理,即使几秒钟,你也不能忍受门上的非素数。
  • 正确!因此,我必须通过一个素数路径发明一个从1033到8179的方案,其中只有一个数字从一个素数变为下一个素数。**

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.

**现在,一直在窃听的财政部长进行了干预。

  • 请不要花费不必要的开支!
    我碰巧知道一个数字的价格是一磅。
  • 嗯,在这种情况下,我需要一个计算机程序来降低成本。
    你不知道一些非常便宜的软件大师,对吗?
  • 事实上,我这样做。
    你看,正在进行这个编程竞赛…帮助总理找到两个给定的四位数素数之间最便宜的素数路径!
    当然,第一个数字必须是非零的。
    这是上述情况的解决方案。
    1033
    1733
    3733
    3739
    3779
    8779
    8179
    该解决方案的成本为6磅。
    请注意,在步骤2中粘贴的数字1在最后一步中无法重复使用 - 必须购买新的1。**

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).

输入
一行带正数:测试用例数(最多100个)。
然后对于每个测试用例,一行用两个数字用空格分隔。
这两个数字都是四位数的素数(没有前导零)。

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
ac代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
using namespace std;
bool prime[12000];   //bool类型数组,储存ture or false
void init()          //这个函数的作用是把从1000到9999的质数挑出来
{
	int key;
	for (int i = 1000; i <= 9999; i++) 
	{
		key = 1;
		for (int j = 2; j < i; j++)   //该循环用来判断一个数是否为质数
		{
			if (i%j == 0) 
			{
				key = 0;
				break;              //避免过多判断
			}
		}
		if (key==1)
			prime[i] = true;      //peime[i]若为质数则为真。
	}
}

bool vis[10000];
int cot[10000];
int bfs(int a, int b)
{
	memset(vis, 0, sizeof(vis));
	memset(cot, 0, sizeof(cot));
	queue <int>qu;                //声明qu是一个整形队列
	vis[a] = 1;                   //把数组vis第a给位置标记为1,代表这个位置有一个数
	qu.push(a);                   //在队列qu的末尾插入元素a。也就是把首质数插入队列qu末尾。
	int bit[10];
	while (~qu.empty())           //返回队列qu是否为空,空则返回1,否则返回0。执行条件为:队伍里面有数据
	{
		int temp = qu.front();   //返回队列qu的第一个元素。把该元素的值赋予给temp
		qu.pop();                //删掉队列qu的第一个元素
		if (temp == b)           //判断temp的值等不等于终质数,执行条件是首质数等于终质数
			return cot[b];       
		bit[1] = temp / 1000; bit[2] = (temp / 100) % 10; bit[3] = (temp / 10) % 10; bit[4] = temp % 10;//bit[1]为首质数的千位,bit[2]为百位依次类推
		for (int bi = 1; bi <= 4; bi++)   //对首质数的各位数开始遍历
		{
			int temp1 = bit[bi];         //把首质数的各位数赋予临时变量temp1;
			for (int i = 0; i < 10; i++) 
			{
				if (bi == 1 && i == 0)   //如果千位数为1
					continue;            
				bit[bi] = i;             
				int sum = bit[1] * 1000 + bit[2] * 100 + bit[3] * 10 + bit[4];   //把各位上的数字还原为一个千位数
				if (prime[sum] && (!vis[sum]))          //执行条件为:sum是质数且在vis这个数组里面第sum个还没存入数据
				{
					vis[sum] = 1;                       //把vis[sum]标记一下,相当于存入数据了
					cot[sum] = cot[temp] + 1;           
					qu.push(sum);                      //在队列qu的末尾插入sum
				}
			}
			bit[bi] = temp1;                           
		}
	}
	return -1;                                        //当无法从首质数变成终质数时,返回-1
}
int main()
{
	init();                          //调用遍历函数 
	int a, b, t;
	cin >> t;
	while (t--) 
	{
		cin >> a >> b;
		int ans = bfs(a, b);
		if (ans == -1)
			cout << "Impossible" << endl;
		else cout << ans<< endl;
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值