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压入队列末端

扫描二维码关注公众号,回复: 5392794 查看本文章

循环直到队列为空
{
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;
	}

}

猜你喜欢

转载自blog.csdn.net/qq_43966202/article/details/86652824