设为首页 加入收藏

TOP

poj3126Prime Path
2015-07-20 18:03:15 来源: 作者: 【 】 浏览:4
Tags:poj3126Prime Path

就因为在判断素数的时候少个等于号 纠结一天,。。。。

?

#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         using namespace std; queue
        
          q; int v[10005],c; int yy[10005]; int qq(int g) { for(int i=2;i*i<=g;i++) { if(g%i==0) return 0; } return 1; } int show(int r) { int n,t; int s=0; v[r]=1; while(!q.empty()) { s++; int y=q.size(); while(y--) { int m=q.front(); q.pop(); n=m/10; for(int i=0;i<=9;i++) { t=n*10+i; if(yy[t]) {if(v[t]==1) continue; else { if(t==c) return s; else q.push(t); } v[t]=1; } } n=m/100; for(int i=0;i<=9;i++) { t=n*100+i*10+m%10; if(yy[t]) {if(v[t]==1) continue; else { if(t==c) return s; else q.push(t); } v[t]=1; } } n=m/1000; for(int i=0;i<=9;i++) { t=n*1000+i*100+m%100; if(yy[t]) {if(v[t]==1) continue; else { if(t==c) return s; else q.push(t); } v[t]=1; } } n=m%1000; for(int i=1;i<=9;i++) { t=n+i*1000; if(yy[t]) {if(v[t]==1) continue; else { if(t==c) return s; else q.push(t); } v[t]=1; } } } } return -1; } int main() { int a,b,d,i; scanf("%d",&a); memset(yy,0,sizeof(yy)); for(i=1000;i<=9999;i++) if(qq(i)) yy[i]=1; while(a--) { memset(v,0,sizeof(v)); scanf("%d %d",&b,&c); if(b==c) { printf("0\n");continue; } q.push(b); d=show(b); if(d==-1) printf("Impossible\n"); else printf("%d\n",d); while(!q.empty()) q.pop(); } return 0; }
        
       
      
     
    
   
  


?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 2669 Romantic 扩展欧几里得 下一篇HDU1312 Red and Black 题解

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: