标签:paste play hose computer stat roo images ide limit
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20237 | Accepted: 11282 |
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
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 7 using namespace std; 8 9 bool prime[10000]; 10 int que[5000]; 11 int step[5000]; 12 int isprime[5000]; 13 bool vis[10000]; 14 int cou=0; 15 16 void getprime(){ 17 int num=10000; 18 num=sqrt(num*1.0); 19 memset(prime,true,sizeof(prime)); 20 prime[0]=prime[1]=false; 21 for(int i=4;i<10000;i+=2){ 22 prime[i]=false; 23 } 24 for(int i=3;i<10000;i+=2){ 25 if(prime[i]){ 26 for(int j=i*i;j<10000;j+=2*i){ 27 prime[j]=false; 28 } 29 } 30 } 31 for(int i=1000;i<10000;i++){ 32 if(prime[i]){ 33 isprime[cou++]=i; 34 } 35 } 36 } 37 38 bool judge(int a,int b){ 39 int sum=0; 40 int t1[4],t2[4]; 41 for(int i=0;i<4;i++){ 42 t1[i]=a%10; 43 a/=10; 44 t2[i]=b%10; 45 b/=10; 46 } 47 for(int i=0;i<4;i++){ 48 if(t1[i]==t2[i]){ 49 sum++; 50 } 51 } 52 if(sum==3){ 53 return true; 54 }else{ 55 return false; 56 } 57 } 58 59 int bfs(int a,int b){ 60 int start=0; 61 int endd=0; 62 memset(vis,true,sizeof(vis)); 63 que[endd]=a; 64 step[endd++]=0; 65 while(start<endd){ 66 int now=que[start]; 67 int nowStep=step[start++]; 68 if(now==b){ 69 return nowStep; 70 } 71 for(int j=0;j<=cou;j++){ 72 int i=isprime[j]; 73 if(!vis[i]){ 74 continue; 75 } 76 if(prime[i]&&judge(i,now)){ 77 if(i==b){ 78 return nowStep+1; 79 } 80 que[endd]=i; 81 step[endd++]=nowStep+1; 82 vis[i]=false; 83 } 84 } 85 } 86 return 0; 87 } 88 89 int main() 90 { 91 int n; 92 scanf("%d",&n); 93 int a,b; 94 getprime(); 95 for(int i=0;i<n;i++){ 96 scanf("%d %d",&a,&b); 97 int ans=bfs(a,b); 98 printf("%d\n",ans); 99 } 100 return 0; 101 }
标签:paste play hose computer stat roo images ide limit
原文地址:http://www.cnblogs.com/TWS-YIFEI/p/6740642.html