HDU 1104 Remainder
2016-01-15 08:53
447 查看
BFS。
#include<cstdio> #include<cstring> #include<queue> #include<string> #include<algorithm> #include<map> #include<iostream> using namespace std; int n,m,k; struct Node { int nowN; int tot; string op; Node(int t,int nown,string o) { tot=t; nowN=nown; op=o; } }; queue<Node>Q; map<int,int>t; int MOD(int a,int b) { if(a>=0) return a%b; if(abs(a)%b==0) return 0; return (a+b*(abs(a)/b+1)); } void BFS() { int flag=0; while(!Q.empty()) Q.pop(); t.clear(); Node s(0,MOD(n,m*k),""); Q.push(s); while(!Q.empty()) { Node head=Q.front(); Q.pop(); if(MOD(n+1,k)==MOD(head.nowN,k)) { flag=1; printf("%d\n",head.tot); cout<<head.op<<endl; break; } if(t[MOD(head.nowN+m,m*k)]==0) { t[MOD(head.nowN+m,m*k)]=1; Node s1(head.tot+1,MOD(head.nowN+m,m*k),head.op+"+"); Q.push(s1); } if(t[MOD(head.nowN-m,m*k)]==0) { t[MOD(head.nowN-m,m*k)]=1; Node s2(head.tot+1,MOD(head.nowN-m,m*k),head.op+"-"); Q.push(s2); } if(t[MOD(head.nowN*m,m*k)]==0) { t[MOD(head.nowN*m,m*k)]=1; Node s3(head.tot+1,MOD(head.nowN*m,m*k),head.op+"*"); Q.push(s3); } if(t[MOD(MOD(head.nowN,m),m*k)]==0) { t[MOD(MOD(head.nowN,m),m*k)]=1; Node s4(head.tot+1,MOD(MOD(head.nowN,m),m*k),head.op+"%"); Q.push(s4); } } if(!flag) printf("0\n"); } int main() { while(~scanf("%d%d%d",&n,&k,&m)) { if(!n&&!m&&!k) break; BFS(); } return 0; }
相关文章推荐
- G面经prepare: Straight Partition of A Deck of Cards
- rails分段查询
- 身体的智能
- Ibatis中的动态SQL:isNotNull,isPropertyAvailable,isNotEmpty用法
- RAID的原理和区别
- main函数参数
- LeetCode_Climbing Stairs--爬楼梯
- [leetCode]70. Climbing Stairs
- codeforces 616E Sum of Remainders(数学公式转化 较难)
- 写的小案例中问题总结--baidumap
- svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
- USACO天梯--Barn Repair
- Climbing Stairs_爬楼梯_递归
- 无法安装vmware tools的解决方PLEASE WAIT! VMware Tools is currently being installed on your system. Dependin
- haartraing 准备(将图像统一修改尺寸)
- VS2010 error: LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
- (Mac)Rails项目使用七牛上传及下载文件
- Studio team goals and pain points
- AIDL的使用
- AjaxControlToolKit--TabContainer控件的介绍收藏[摘录]