POJ 3126 Prime Path(素数变换路径)

来源:互联网 发布:vb format函数 编辑:程序博客网 时间:2024/05/18 01:49

题目:

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

31033 81791373 80171033 1033

Sample Output

670

这个题目是用广度优先搜索。

每次都出队得到一个数,然后对和这个数只隔了一个数码的数(一共有8+9+9+9=35个)进行判断,如果是素数那么就进队。

这样我开始的代码是:

#include<iostream>#include<queue>using namespace std;int n, a, b;int list[10000];queue<int>q;bool is_prime(int n){if (n % 2 == 0)return false;for (int i = 3; i*i <= n; i += 2)if (n%i == 0)return false;return true;}void f(int t){if (is_prime(t) && t != a){q.push(t);list[t] = list[a] + 1;}}int main(){int a1, a2, a3, a4;cin >> n;while (n--){cin >> a >> b;q.push(a);int sum = 0;memset(list, -1, sizeof(list));list[a] = 0;while (!q.empty() && list[b]<0){a = q.front();//a=a1a2a3a4q.pop();a4 = a % 10;a3 = (a / 10) % 10;a2 = (a / 100) % 10;a1 = a / 1000;int t;for (int i = 1; i < 10; i++)f(a - a1 * 1000 + i * 1000);for (int i = 0; i < 10; i++){f(a - a2 * 100 + i * 100);f(a - a3 * 10 + i * 10);f(a - a4 + i);}}cout << list[b] << endl;}return 0;}

运行的结果是

4
1033 8179
1873
1373 8017
55
1033 1033
0

发现结果非常大。

然后我把函数改成了

void f(int t){     if (is_prime(t) && t != a)    {          q.push(t);          if(list[t]<0)list[t] = list[a] + 1;     }}

运行结果是

4
1033 8179
6
1373 8017
4
1033 1033
0

然后再仔细看这个函数,发现还是不对,又改成了

void f(int t){if (is_prime(t) && t != a && list[t] < 0){q.push(t);list[t] = list[a] + 1;}}
在进队之前就判断有没有被访问过。

其实很明显就应该这样,但是以前我对队列不熟,对广度优先也不熟,很多问题都是用深度优先的回溯做的。

运行结果是

4
1033 8179
6
1373 8017
8
1033 1033
0

只有中间的那一组是错的,单独运行

2
1373 8017
7

结果又是对的。

所以我认为是队列没有清空的原因。

加了一句 while(q.size())q.pop(); 之后

代码变成:

#include<iostream>#include<queue>using namespace std;int n, a, b;int list[10000];queue<int>q;bool is_prime(int n)//判断是不是素数{if (n % 2 == 0)return false;for (int i = 3; i*i <= n; i += 2)if (n%i == 0)return false;return true;}void f(int t)//这个函数是用来进队的,写这个函数只是为了节约重复代码{if (is_prime(t) && t != a && list[t] < 0){q.push(t);list[t] = list[a] + 1;}}int main(){int a1, a2, a3, a4;cin >> n;while (n--){cin >> a >> b;while(q.size())q.pop();//清空队列q.push(a);int sum = 0;memset(list, -1, sizeof(list));//初始化listlist[a] = 0;while (!q.empty()&&list[b]<0){a = q.front();//a=(a1a2a3a4)10进制q.pop();a4 = a % 10;a3 = (a / 10) % 10;a2 = (a / 100) % 10;a1 = a / 1000;int t;for (int i = 1; i < 10; i++)f(a - a1 * 1000 + i * 1000);for (int i = 0; i < 10; i++){f(a - a2 * 100 + i * 100);f(a - a3 * 10 + i * 10);f(a - a4 + i);}}cout << list[b] << endl;}return 0;}


然后运行结果对了,一提交,果然是对的。
2 0
原创粉丝点击