標籤:oj poj bfs algorithm acm
來源:http://poj.org/problem?id=3126
Prime Path
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
| Total Submissions: 11384 |
|
Accepted: 6453 |
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
Source Northwestern Europe 2006 |
題目大意: 從素數四位元a變換到四位元b,每次只能改變一位元字,且每次改變後的數也必須為素數~~ 四位元中第一位不能為0,求最少變換次數,如果無法變換,則輸出"Impossible".
題解:明顯的廣搜~~ 每次改變一位元,用hash判重解決之。
PS:這題出現了一個很蛋疼的錯誤,昨天範例都可以過, 但是提交WA。今天重寫一遍-----發現把++exdir 寫成exidir++囧..........
AC代碼:
#include<iostream>#include<cmath>#include<cstring>using namespace std;const int Max=2500000,Maxh=10005;bool visit[Maxh]={0},Isprime[Maxh]={0};int star,dest;bool prime(int k){ for(int i=2;i<=(int)sqrt((float)k);i++) if(k%i==0) return false; return true;}struct Node{ int dig[4],num,step;}map[Max]={0},temp;void calc(Node &x){x.dig[0]=x.num/1000;x.dig[1]=(x.num/100)%10;x.dig[2]=(x.num/10)%10;x.dig[3]=x.num%10;}void calcsum(Node &x){ x.num=0; for(int i=0;i<4;i++) x.num+=x.dig[i]*(int)pow((float)10,3-i);}int main(){ int t; cin>>t; for(int i=1000;i<10000;i++) if(prime(i)) Isprime[i]=1; while(t--){ memset(visit,0,sizeof(visit)); int flag=1; cin>>star>>dest; if(star==dest){ cout<<0<<endl; continue; } map[0].num=star; calc(map[0]); map[0].step=0; int nodedir=0,exdir=0; while(nodedir<=exdir&&exdir<Max&&flag){ for(int i=0;i<4&&flag;i++){ for(int k=0;k<10;k++){ if(i||k){ if(i==3&&k%2==0) continue; temp=map[nodedir]; temp.dig[i]=k; calcsum(temp); temp.step++; if(temp.num==dest){ cout<<temp.step<<endl; flag=0; break; } if(Isprime[temp.num]&&!visit[temp.num]){ visit[temp.num]=1; map[++exdir]=temp; } } } } nodedir++; } if(flag) cout<<"Impossible"<<endl; }return 0;}