标签:des style blog http color os strong io
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11288 | Accepted: 6398 |
Description
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
题意:给出两个素数,问至少需要多少步将第一个转换成第二个。每次只能转换一位。并且第一位不能是零。
思路:广搜枚举每一位,每一位都9或10种情况。水题。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : PrimePath.cpp 4 * Creat time : 2014-08-03 16:07 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 15000 15 using namespace std; 16 int isprime[M+5],vis[M],cnt[M]; 17 int a,b; 18 void MakePrime() 19 { 20 clr(isprime,0); 21 isprime[0] = isprime[1] = 1; 22 for(int i = 2; i < M; i++){ 23 if(!isprime[i]){ 24 for(int j = i+i; j < M; j+=i){ 25 isprime[j] = 1; 26 } 27 } 28 } 29 } 30 void BFS(int s) 31 { 32 clr(vis,0); 33 clr(cnt,0); 34 vis[s] = 1; 35 queue<int>que; 36 que.push(s); 37 while(!que.empty()){ 38 int t = que.front(); 39 que.pop(); 40 if(t == b){ 41 printf("%d\n",cnt[t]); 42 break; 43 } 44 /*------------------处理第1位------------------*/ 45 int no1 = t/1000; 46 int temp = t - no1*1000; 47 for(int j = 1; j <= 9; j++){ 48 if(j == no1) continue; 49 int change_num = j*1000+temp; 50 if(!isprime[change_num] && !vis[change_num]){ 51 que.push(change_num); 52 vis[change_num] = 1; 53 cnt[change_num] = cnt[t] + 1; 54 } 55 } 56 /*---------------------end----------------------*/ 57 /*------------------处理第2位-------------------*/ 58 int no2 = t/100%10; 59 int s2 = t%100; 60 for(int j = 0; j <= 9; j++){ 61 if(j == no2) continue; 62 int change_num = no1*1000+j*100+s2; 63 if(!isprime[change_num] && !vis[change_num]){ 64 que.push(change_num); 65 vis[change_num] = 1; 66 cnt[change_num] = cnt[t] + 1; 67 } 68 } 69 /*---------------------end----------------------*/ 70 /*------------------处理第3位-------------------*/ 71 int no3 = t/10%10; 72 s2 = t/100; 73 int s1 = t%10; 74 for(int j = 0; j <= 9; j++){ 75 if(j == no3) continue; 76 int change_num = s2*100+j*10+s1; 77 if(!isprime[change_num] && !vis[change_num]){ 78 que.push(change_num); 79 vis[change_num] = 1; 80 cnt[change_num] = cnt[t] + 1; 81 } 82 } 83 /*---------------------end---------------------*/ 84 /*-----------------处理第4位-------------------*/ 85 int no4 = t%10; 86 int s3 = t-no4; 87 for(int j = 0; j <= 9; j++){ 88 if(j == no4) continue; 89 int change_num = s3 + j; 90 if(!isprime[change_num] && !vis[change_num]){ 91 que.push(change_num); 92 vis[change_num] = 1; 93 cnt[change_num] = cnt[t] + 1; 94 } 95 } 96 /*--------------------end----------------------*/ 97 } 98 } 99 int main(int argc,char *argv[]) 100 { 101 MakePrime(); 102 int t; 103 scanf("%d",&t); 104 while(t--){ 105 scanf("%d%d",&a,&b); 106 BFS(a); 107 } 108 return 0; 109 }
poj 3126 -- Prime Path,布布扣,bubuko.com
标签:des style blog http color os strong io
原文地址:http://www.cnblogs.com/ubuntu-kevin/p/3888571.html