hdu 4474 搜索bfs
2014-10-31 10:42
471 查看
Yet Another Multiple Problem
Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3740 Accepted Submission(s): 907
[align=left]Problem Description[/align]
There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
[align=left]Input[/align]
There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
[align=left]Output[/align]
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist
such a multiple.
[align=left]Sample Input[/align]
2345 3 7 8 9 100 1 0
[align=left]Sample Output[/align]
Case 1: 2345 Case 2: -1
[align=left]Source[/align]
2012 Asia Chengdu Regional Contest
/*题意: 给一个n(n<=10000), m个数字(长度为1),问最小的数x(x%n=0) 不包含这m个数。 思路: 直接求,没想出解法. 对于一个数 x%n = m, 则 x` = x*10+i , 有 m` = (m*10+i)%n 我们可以利用 除了m个数字外的数来构造这个 X. 因为需要最小的, 则其长度与字典序排列皆最小. 通过BFS进行搜索. 每一个模n的余数只取第一次出现的, 因为之后再出现的. 只可能更长或者更大. 必定不是最优解. 搜索节点,保存三个信息: 当前x的最后一位, 当前x%n的余数, 当前节点的父亲节点. 结果输出利用记忆父亲节点 ,然后递归输出即可.*/ #include<stdio.h> #include<queue> #include<string.h> #define N 10005 using namespace std; int b ,pre ,mark ,n; void print(int k) { if(pre[k]) print(pre[k]); printf("%d",b[k]); } void bfs() { queue<int>q; int i,r,x,y; for(i=1; i<10; i++) { if(!mark[i]) { r=i%n; if(r==0) { printf("%d\n",i); return; } else if(pre[r]==-1) { pre[r]=0;//pre指向前驱,此时无前驱 q.push(r);//根节点保存的mod值 b[r]=i;//保存i,用于输出 } } } while(!q.empty()) { x=q.front(); q.pop(); for(i=0; i<10; i++) { if(!mark[i]) { y=x*10+i; r=y%n; if(r==0) { print(x); printf("%d\n",i); return; }//表示没有r这个mod没有访问过,则把它加入队列 if(pre[r]==-1) { q.push(r); b[r]=i; pre[r]=x; } } } } printf("-1\n"); } int main() { int m,i,a,cnt=1; while(scanf("%d%d",&n,&m)!=EOF) { memset(mark,0,sizeof(mark)); memset(pre,-1,sizeof(pre)); for(i=1; i<=m; i++) { scanf("%d",&a); mark[a]=1; } printf("Case %d: ",cnt++); bfs(); } return 0; }
相关文章推荐
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索 根据剩余类建图广搜
- HDU-4474 Yet Another Multiple Problem BFS搜索
- HDU 4474 Yet Another Multiple Problem(搜索 BFS)
- HDU_4474_数位bfs——经典搜索
- hdu 4474 数位搜索bfs
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索
- HDU 1253 三维搜索bfs
- HDU 4474 BFS
- HDU 1429 BFS+状态搜索
- 【搜索之BFS】杭电 hdu 1240 Asteroids!
- HDU 1175(搜索题,BFS)
- 交换地方HDU 1195 Open the Lock 简单搜索-bfs
- 【搜索之BFS】杭电 hdu 1548 A strange lift
- HDU 2612(搜索题,BFS)
- HDU_1242——二维空间搜索,使用优先队列BFS
- HDU 1180(搜索题,BFS)
- hdu 1254 推箱子 很好的嵌套搜索 双层BFS
- HDU-4394 Digital Square BFS搜索
- hdu 1043 八数码 经典搜索问题 BFS+MAP
- BFS三维搜索--HDU 1253