Prime Path(bfs)

来源:互联网 发布:淘宝付款显示交易关闭 编辑:程序博客网 时间:2024/05/16 18:50
H - Prime Path
Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u
SubmitStatusPractice POJ 3126
Appoint description:

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
题意:
给你两个素数n,m,求n变到m要多少步。每次只改变一位,所得数必须也是素数,并且每个素数只能出现一次
思路:
这题看懂题目后就是一道水题,因为只有四位,所以只要用bfs搜一次就能在 最短的时间内找到了,符合的数了
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>#include<queue>typedef unsigned long long ll;using namespace std;#define T 10005#define inf 0x3f3f3f3fint n,m;bool prime[T];bool vis[T];void Isprime(){memset(prime,false,sizeof(prime));for(int i=2;i*i<=T;++i){if(!prime[i])for(int j=i<<1;j<=T;j+=i){prime[j] = true;}}}struct node{int x[4],c;int sum(){return x[0]*1000+x[1]*100+x[2]*10+x[3];}}v[T],a,b;int bfs(){int first=0,second=0;v[second].x[0] = n/1000;v[second].x[1] = n/100%10;v[second].x[2] = n/10%10;v[second].x[3] = n%10;v[second++].c = 0;vis[n] = true;while(first!=second){a = v[first++];if(a.sum()==m)return a.c;for(int i=0;i<4;++i){for(int j=0;j<=9;++j){b = a;b.x[i] = j;b.c = a.c +1;int tmp = b.sum();if(j==0&&i==0||vis[tmp]||prime[tmp])continue;if(tmp==m)return b.c;v[second++] = b;vis[tmp] = true;}}}return -1;}int main(){#ifdef zsc freopen("input.txt","r",stdin);#endif  int N,k; Isprime(); scanf("%d",&N); while(N--) {scanf("%d%d",&n,&m);memset(vis,false,sizeof(vis));k = bfs();if(k==-1)printf("Impossible\n");elseprintf("%d\n",k); }    return 0;}

0 0
原创粉丝点击