http://poj.org/problem?id=3126
我的第一道BFS,WA了几次,TLE了几次,最后AC!
Time Limit:1000MS | Memory Limit:65536K | |
Total Submissions:5022 | Accepted:2908 |
Description

— 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.
1033The 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.
1733
3733
3739
3779
8779
8179
Input
Output
Sample Input
3 1033 8179 1373 8017 1033 1033
Sample Output
6 7 0
/* Author : yan * Question : POJ 3126 Prime Path * Data : Sunday, December 19 2010 */ //POJ的第一道BFS #include<stdio.h> #define bool _Bool #define true 1 #define false 0 #define QueueMaxSize 400 //定义队列同时允许的最大值 int prime_tab[1065];//1000--10000的素数表 bool have_queued[10000];//是否访问过标记 int step[10000];//记录每个元素的步数 int tmp;//临时变量 int queue[QueueMaxSize];//队列数组 int head,rear;//队头和队尾 int index;//素数表的最大值 int is_prime(int n) { int i; tmp=sqrt(n); for(i=2;i<=tmp;i++) if(n%i==0) return 0; return 1; } /* **判断两个数字是不是只有一位不同,很重要,我自己写的提交的时候TLE,由于先转换成字符串,导致速率很慢 ////////// bool differ_a_bit(int a,int b) { sprintf(cache1,"%d",a); sprintf(cache2,"%d",b); int _i; int cnt=0; for(_i=0;cache1[_i]!='/0'&&cache2[_i]!='/0';_i++) if(cache1[_i]!=cache2[_i]) cnt++; if(cnt==1) return true; return false; } ////////// */ bool differ_a_bit(int a,int b) { int flag = 0; while(a){ if(a%10 != b%10)flag++; a/=10; b/=10; } if(flag == 1)return true; return false; } /* **循环队列初始化函数 */ void initial() { head=0;rear=0; } /* **判断队列是否为空 */ bool isEmpty() { if(head==rear) return true; return false; } /* **将值入队操作 */ int enQueue(int value) { queue[rear]=value; rear=(rear+1)%QueueMaxSize; have_queued[value]=1; return value; } /* **元素出队,返回出队的元素 */ int deQueue() { if(head==rear) return -1; else { int cache=head; head=(head+1)%QueueMaxSize; return queue[cache]; } } /* **返回对头元素而不出队 */ int headValue() { if(!isEmpty()) return queue[head]; return -1; } /* **广度优先搜索函数,参数:开始和结束 */ int BFS(int start,int end) { enQueue(start); int _head; int _i; while(!isEmpty()) { _head=deQueue(); for(_i=0;_i<index;_i++) { //printf("%d/n",_head); if( differ_a_bit( prime_tab[_i] , _head ) && have_queued[prime_tab[_i]]==0 ) { step[prime_tab[_i]]=step[_head]+1; if(end==_head) return 1; enQueue(prime_tab[_i]); } } } return 0; } int main() { //freopen("input","r",stdin); int i,j; int test; int p1,p2; int pos1,pos2; int cnt; for(i=1001;i<9999;i++) if(is_prime(i)) prime_tab[index++]=i; //printf("index=%d/n",index); scanf("%d",&test); while(test--) { cnt=0; memset(have_queued,0,sizeof(have_queued)); memset(step,0,sizeof(step)); initial(); scanf("%d %d",&p1,&p2); BFS(p1,p2); printf("%d/n",step[p2]); } //printf("%d ",differ_a_bit(700)); return 0; }