设为首页 加入收藏

TOP

hdu1104 Remainder bfs找算式是否有解……
2015-07-20 18:07:13 来源: 作者: 【 】 浏览:5
Tags:hdu1104 Remainder bfs 算式 是否

需要注意的是,进行模运算剪枝……

#include
  
   
#include
   
     #include
    
      #include
     
       using namespace std; int N,M,K,T,X; bool used[1010]; struct node { int value,step; char prc[10000]; }st; int remind(int a,int b) { return (a%b+b)%b; } void init() { T=remind(N+1,K); st.value=N; X=M*K; st.step=0; memset(used,0,sizeof(used)); used[st.value]=1; } void bfs() { int i,tmp,tmp1; queue
      
       qq; node t1,t2; qq.push(st); while(qq.size()) { t1=qq.front(); qq.pop(); if(remind(t1.value,K)==T) { t1.prc[t1.step]='\0'; printf("%d\n%s\n",t1.step,t1.prc); return; } for(i=0;i<4;i++) { t2=t1; t2.step++; if(i==0) { tmp=(t2.value+M)%X; t2.prc[t1.step]='+'; } else if(i==1) { tmp=(t2.value-M)%X; t2.prc[t1.step]='-'; } else if(i==2) { tmp=(t2.value*M)%X; t2.prc[t1.step]='*'; } else { tmp=remind(t2.value,M); t2.prc[t1.step]='%'; } tmp1=remind(tmp,K); if(!used[tmp1]) { used[tmp1]=1; t2.value=tmp; qq.push(t2); } } } printf("0\n"); } int main() { while(scanf("%d%d%d",&N,&K,&M)&&(N!=0||M!=0||K!=0)) { init(); bfs(); } }
      
     
    
   
  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA 23 out of 5 下一篇编程算法 - 求1+2+...+n(函数指针..

评论

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