标签:using std const ber cab blank algo ppi either
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21581 | Accepted: 11986 |
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 "cstdio" 2 #include "iostream" 3 #include "algorithm" 4 #include "string" 5 #include "cstring" 6 #include "queue" 7 #include "cmath" 8 #include "vector" 9 #include "map" 10 #include "stdlib.h" 11 #include "set" 12 #define mj 13 #define db double 14 #define ll long long 15 using namespace std; 16 const int N=1e4+5; 17 const int mod=1e9+7; 18 const ll inf=1e16+10; 19 bool pri[N]; 20 bool u[N],v[N]; 21 int c[N];//统计步数 22 void init(){ 23 int i,j; 24 for(i=1000;i<=N;i++){ 25 for(j=2;j<i;j++) 26 if(i%j==0){ 27 pri[i]=0; 28 break; 29 } 30 if(j==i) pri[i]=1; 31 } 32 } 33 int bfs(int s,int e){ 34 queue<int >q; 35 memset(v,0, sizeof(v)); 36 memset(c,0, sizeof(c)); 37 int tmp,a[4],ans=s; 38 q.push(s); 39 v[s]=1; 40 while(q.size()){ 41 int k=q.front(); 42 q.pop(); 43 a[0]=k/1000,a[1]=k/100%10,a[2]=k/10%10,a[3]=k%10; 44 for(int i=0;i<4;i++){ 45 tmp=a[i]; 46 for(int j=0;j<10;j++){ 47 if(tmp!=j){ 48 a[i]=j; 49 ans=a[0]*1000+a[1]*100+a[2]*10+a[3]; 50 if(pri[ans]&&!v[ans]){//为素数且未使用过 51 c[ans]=c[k]+1; 52 v[ans]=1; 53 q.push(ans); 54 } 55 if(ans==e) { 56 printf("%d\n",c[ans]); 57 return 0; 58 } 59 } 60 a[i]=tmp; 61 } 62 } 63 if(k==e) { 64 printf("%d\n",c[k]); 65 return 0; 66 } 67 } 68 printf("Impossible\n"); 69 return 0; 70 } 71 int main() 72 { 73 int n; 74 int x,y; 75 scanf("%d",&n); 76 init(); 77 for(int i=0;i<n;i++){ 78 scanf("%d%d",&x,&y); 79 bfs(x,y); 80 } 81 return 0; 82 }
标签:using std const ber cab blank algo ppi either
原文地址:http://www.cnblogs.com/mj-liylho/p/7201021.html