remainder(bfs路径打印)
来源:互联网 发布:蒙自网络问政平台 编辑:程序博客网 时间:2024/05/29 07:49
Remainder
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 2 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Coco is a clever boy, who is good at mathematics. However, he is puzzled by a difficult mathematics problem. The problem is: Given three integers N, K and M, N may adds (‘+’) M, subtract (‘-‘) M, multiples (‘*’) M or modulus (‘%’) M (The definition of ‘%’ is given below), and the result will be restored in N. Continue the process above, can you make a situation that “[(the initial value of N) + 1] % K” is equal to “(the current value of N) % K”? If you can, find the minimum steps and what you should do in each step. Please help poor Coco to solve this problem.
You should know that if a = b * q + r (q > 0 and 0 <= r < q), then we have a % q = r.
Input
There are multiple cases. Each case contains three integers N, K and M (-1000 <= N <= 1000, 1 < K <= 1000, 0 < M <= 1000) in a single line.
The input is terminated with three 0s. This test case is not to be processed.
Output
For each case, if there is no solution, just print 0. Otherwise, on the first line of the output print the minimum number of steps to make “[(the initial value of N) + 1] % K” is equal to “(the final value of N) % K”. The second line print the operations to do in each step, which consist of ‘+’, ‘-‘, ‘’ and ‘%’. If there are more than one solution, print the minimum one. (Here we define ‘+’ < ‘-‘ < ‘’ < ‘%’. And if A = a1a2…ak and B = b1b2…bk are both solutions, we say A < B, if and only if there exists a P such that for i = 1, …, P-1, ai = bi, and for i = P, ai < bi)
Sample Input
2 2 2
-1 12 10
0 0 0
Sample Output
0
2
*+
Author
Wang Yijie
给你一个n,k,m,然后按要求通过(n(’+,-,*,%’)m)%k,来使得其最终值等于(n+1)%k。
注意一点,mod是左操作数为正时用的,所以在标记路径时用(n%k+k)%k。
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <queue>#define N 1000010using namespace std;int n,k,m,km;/*int yunsuan(int a,int b,char s){ if(s=='+') return a+b; if(s=='-') return a-b; if(s=='*') return a*b; if(s=='%') return a%b; return 0;}char dir[4]={'+','-','*','/'}int dfs(int ck,int m, char s){ int ans=0; int i,j; if((vk+1)%m==ck) { printf("%d\n",ans); for(i=0;i<ans;i++) { if(i) printf(" %c",c[i]); else printf("%c",c[i]); } printf("\n"); } else { for(i=0;;i++) { if(vk!=ck) { ck=yunsuan(ck,m,dir[i]); c[ans++]=dir[i]; dfs(ck,m,dir[i]); } if(vk==ck) break; } } return 0;}int main(){ int i,j; while(scanf("%d%d%d",&n,&k,&m)!=EOF&&(n+k+m)) { dfs(,) } return 0;}*/int vist[N];struct node{ int num,step; string road;};void bfs(){ int i; node st,ed; memset(vist,0,sizeof(vist)); queue<node>Q; st.num=n; st.step=0; st.road=""; vist[(n%k+k)%k]=1; Q.push(st); while(!Q.empty()) { st=Q.front(); Q.pop(); if(((n+1)+k)%k==(st.num%k+k)%k) { printf("%d\n",st.step); cout<<st.road<<endl; return; } ed.step=st.step+1; for(i=0; i<4; i++) { if(i==0) { ed.num=(st.num+m)%km; ed.road=st.road+'+'; } else { if(i==1) { ed.num=(st.num-m)%km; ed.road=st.road+'-'; } else { if(i==2) { ed.num=(st.num*m)%km; ed.road=st.road+'*'; } else { ed.num=(st.num%m+m)%m%km; ed.road=st.road+"%"; } } } if(!vist[(ed.num%k+k)%k]) { Q.push(ed); vist[(ed.num%k+k)%k]=1; } } } puts("0");}int main(){ while(scanf("%d%d%d",&n,&k,&m)!=EOF&&(n+k+m)) { km=k*m; bfs(); } return 0;}
- remainder(bfs路径打印)
- Remainder(bfs + 记录路径)
- hdu-1104-Remainder(BFS打印路径+数论)(%与mod的区别)
- HDU 1104 Remainder (BFS)
- HDOJ1104 Remainder(数论+BFS)
- bfs--hdoj1026(打印路径)
- hdu1026(BFS,打印路径)
- HDU 1104题Remainder(bfs)
- hdu 1104 Remainder(BFS+数论)
- HDOJ 1104 Remainder(BFS + 数论)
- POJ 3414 Pots ( BFS , 打印路径 )
- 广度优先搜索(BFS)+路径打印
- POJ -3414 Pots(BFS + 打印路径)
- hdu1026(bfs+dfs打印路径)
- POJ-3414 POTS(BFS打印路径)
- BFS(简单路径打印)
- bfs路径打印
- 1104 Remainder(数论+bfs)
- 定义接口&监听回调
- 括号匹配
- zynq 驱动
- 【HDU】5763 Another Meaning
- 【CDP-云设计模式】第5章,5.重命名分配模式(Rename Distribution Pattern)
- remainder(bfs路径打印)
- centOS 开机自启动配置
- 小实验~图像在matlab和opencv mat里的存储
- Block 的原理
- HDU 2181哈密顿绕行世界问题 (简单DFS)
- linux下的redis环境搭建及主从配置
- Ubuntu16.04最快捷搭建小型局域网Git服务器
- Spring-AOP:基于AspectJ注解的AOP
- MBProgressHUD的基本使用