标签:poj
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12974 | Accepted: 7342 |
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
#include <stdio.h> #include <queue> #include <string.h> using namespace std; #define HUR 100 #define THO 1000 #define TEN 10 const int maxn=10000; bool prime[maxn+5]; int vis[maxn],s,e; void prime_table(){ int i,j; memset(prime,0,sizeof(prime)); for(i=2;i<maxn;i++) if(!prime[i]) for(j=i*i;j<maxn;j+=i) prime[j]=1; } int bfs(){ int i; memset(vis,0,sizeof(vis)); vis[s]=1; queue<int> que; que.push(s); while(!que.empty()){ int t=que.front(); que.pop(); int d=t; d%=1000; for(i=1;i<10;i++){ int tt=d+i*THO; //变换千位 if(prime[tt]==0 && vis[tt]==0){ if(tt==e) return vis[t]; que.push(tt);vis[tt]=vis[t]+1; } } d=t%100+(t/1000*1000); for(i=0;i<10;i++){ int tt=d+i*HUR; //变换百位 if(prime[tt]==0 && vis[tt]==0){ if(tt==e) return vis[t]; que.push(tt);vis[tt]=vis[t]+1; } } d=t%10+t/100*100; for(i=0;i<10;i++){ int tt=d+i*TEN; //变换十位 if(prime[tt]==0 && vis[tt]==0){ if(tt==e) return vis[t]; que.push(tt);vis[tt]=vis[t]+1; } } d=t/10*10; for(i=0;i<10;i++){ int tt=d+i; //变换个位 if(prime[tt]==0 && vis[tt]==0){ if(tt==e) return vis[t]; que.push(tt);vis[tt]=vis[t]+1; } } } return 0; } int main() { int T,res; prime_table(); scanf("%d",&T); while(T--){ scanf("%d%d",&s,&e); if(s==e){ printf("0\n"); continue; } res=bfs(); if(res==0) printf("Impossible\n"); else printf("%d\n",res); } return 0; }
标签:poj
原文地址:http://blog.csdn.net/u013068502/article/details/45487497