poj 3126 Prime Path
2016-09-11 15:18
141 查看
Prime Path
Description
![](http://poj.org/images/3126_1.jpg)
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
Sample Output
Source
这一个题的题意是
给定两个素数a b,求a变幻到b最少需要几步
并且变幻时只有一个数字不同,并且是素数
比方说上面讲的1033不能变化到8033因为8033不是素数,
解题思想就是
首先要对素数进行打表
然后用bfs一个一个的改变位数的值进行判断就行了,
还要知道的是一个素数到另一个素数是一定能实现的,所以就不用考虑不能实现怎么办的问题了。
代码如下
如有帮助记得支持我一下,谢谢!!!
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17651 | Accepted: 9941 |
![](http://poj.org/images/3126_1.jpg)
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
这一个题的题意是
给定两个素数a b,求a变幻到b最少需要几步
并且变幻时只有一个数字不同,并且是素数
比方说上面讲的1033不能变化到8033因为8033不是素数,
解题思想就是
首先要对素数进行打表
然后用bfs一个一个的改变位数的值进行判断就行了,
还要知道的是一个素数到另一个素数是一定能实现的,所以就不用考虑不能实现怎么办的问题了。
代码如下
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <queue> #include <algorithm> using namespace std; struct node { int step; int x; }; int Prime[10002]; int repeat[10002]; int t[4]; queue <node> q; void change(int x) { t[0] = x%10; t[1] = x/10%10; t[2] = x/100%10; t[3] = x/1000; } void init() { int i, j; int num; for ( i = 1001; i < 10000;i = i+2 ) { num = sqrt(i+0.5); for ( j = 2;j <= num; j++ ) { if( i%j == 0 ) { Prime[i] = 0; break; } } if ( j > num ) { Prime[i] = 1; } } } int bfs( int n, int m ) { int i; while( !q.empty() ) { q.pop(); } node temp; temp.x = n; temp.step = 0; q.push(temp); while ( !q.empty() ) { node tail; temp = q.front(); q.pop(); change(temp.x); for ( i = 1;i < 10; i = i+2) { tail.x = i+t[1]*10+t[2]*100+t[3]*1000; if (Prime[tail.x] == 1&&repeat[tail.x] == 0) { repeat[tail.x] = 1; tail.step = temp.step+1; if ( tail.x == m ) return tail.step; q.push(tail); } } for ( i = 0;i < 10; i++) { tail.x = t[0]+i*10+t[2]*100+t[3]*1000; if (Prime[tail.x] == 1&&repeat[tail.x] == 0) { repeat[tail.x] = 1; tail.step = temp.step+1; if ( tail.x == m ) return tail.step; q.push(tail); } } for ( i = 0;i < 10; i++) { tail.x = t[0]+t[1]*10+i*100+t[3]*1000; if (Prime[tail.x] == 1&&repeat[tail.x] == 0) { repeat[tail.x] = 1; tail.step = temp.step+1; if ( tail.x == m ) return tail.step; q.push(tail); } } for ( i = 1;i < 10; i++) { tail.x = t[0]+t[1]*10+t[2]*100+i*1000; if (Prime[tail.x] == 1&&repeat[tail.x] == 0) { repeat[tail.x] = 1; tail.step = temp.step+1; if ( tail.x == m ) return tail.step; q.push(tail); } } } return -1; } int main() { int n, m, T; init(); scanf ( "%d", &T ); while ( T-- ) { memset(repeat, 0, sizeof(repeat)); scanf ( "%d %d", &n, &m ); if ( n == m ) { printf ("0\n"); continue; } int sum = bfs( n, m ); printf ( "%d\n", sum ); } }代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!
相关文章推荐
- BFS-POJ-3126-Prime Path
- POJ 3126-Prime Path【搜索】
- poj 3126 Prime Path
- POJ-3126 Prime Path ( BFS )
- POJ 3126 Prime Path
- POJ 3126 Prime Path(素数变换路径)
- POJ 3126, Prime Path
- Prime Path (POJ - 3126) (BFS)
- poj 3126 Prime Path(BFS)
- poj 3126 Prime Path
- POJ 3126 - Prime Path(BFS)
- BFS POJ 3126 Prime Path
- POJ 3126 Prime Path (BFS)
- POJ 3126 - Prime Path
- POJ 3126 Prime Path(BFS)
- ACM篇:POJ--3126 Prime Path
- POJ 3126 Prime Path (BFS求最少步数)
- POJ 3126 Prime Path(BFS)
- 【poj 3126】 Prime Path 广搜
- poj 3126 Prime Path(简单题)