HDU4474(搜索思想)
来源:互联网 发布:spss时间序列数据预测 编辑:程序博客网 时间:2024/05/17 12:23
先贴一发,还不是太理解
/** * 比较巧的广搜+记录最短路径 * 搜余数,骚的一匹。。。 * 用余数做结点,复杂度10000 * 复习题 * n1 mod k = (n1 mod k) mod k * (n1 + n2) mod k = ((n1 mod k) + (n2 mod k)) mod k * (n1 * w) mod k = ((n1 mod k) * w) mod k * 已保证先搜到的是最小的。 */#include<iostream>#include<cstdio>#include<queue>#include<algorithm>#include<cstring>using namespace std;const int mmax = 10002;bool book[mmax];bool unincluded[11];int n;char route[mmax];//字符串输出,每个下标(余数)只存一位数,也可以用intint pre[mmax];bool bfs(){ queue<int> Q; Q.push(0);//先放一个数,为何放0 int now;//代表余数,其实本来是要代表的未取余的数,但经过证明可以用余数代替。 while(!Q.empty()) { now = Q.front(); Q.pop(); for(int i = 0; i < 10; i++)//必须从0开始,遍历数字0-9 { if(unincluded[i])//不允许使用的数字 continue; if(now == 0 && i == 0)//排除第一个?? continue; int next = (now * 10 + i)%n;//下一个余数 if(book[next])//如果余数用过,则不加入,剪枝 continue; route[next] = '0' + i; book[next] = true; pre[next] = now;//记录到达这个余数前面的余数 Q.push(next); if(next == 0)//可以放到开头 { return true; } } } return false;}void out(){ int p = 0; string output; while(p != 0 || output.empty()) { output += route[p]; p = pre[p]; } reverse(output.begin(), output.end()); puts(output.c_str());//c语言遗留的,书上写的,返回指针}int t;int main(){ int m; t = 1; while(scanf("%d%d", &n, &m) != EOF) { memset(unincluded, 0, sizeof(unincluded)); memset(book, 0, sizeof(book)); int num; for(int i = 1; i <= m; i++) { scanf("%d", &num); unincluded[num] = true; } printf("Case %d: ", t); if(bfs()) { out(); } else { printf("-1\n"); } t++; } return 0;}
阅读全文
0 0
- HDU4474(搜索思想)
- hdu4474 bfs搜索
- HDU4474
- 记忆化搜索(搜索+dp思想)
- HDU4474(BFS)
- hdu4474 bfs
- hdu4474 bfs
- 搜索+离散化思想
- 小搜索思想
- hdu4474 数位广搜
- HDU4474(数位BFS)
- 找朋友(bfs)搜索(还有队列思想)
- 二分搜索及其思想(挑战例题总结)
- HDU FatMouse and Cheese (记忆化搜索+dp思想)
- 【HDU4474 2012成都现场赛K】【搜索-BFS】Yet Another Multiple Problem 最小的要求字符集的数字倍数 余数哈希BFS
- 广度优先搜索的思想
- IconManageBox软件:搜索查询思想
- BFS宽度优先搜索思想
- Talend中,组件放置顺序也会让程序运行出问题,调取更多组件方法
- halcon软件简单规则
- 监督学习四 基于实例的学习
- js获取浏览器窗口大小(调样式中,,,)
- SpringBoot中加密包对配置文件中的密码进行加密
- HDU4474(搜索思想)
- Ubuntu系统获取root权限安装vim编辑器
- 动画
- 数据结构实验之栈四:括号匹配
- 机器学习到底在量化金融里的哪些方面有应用?
- 无限轮播+小圆点
- BRE与ERE的差异
- post请求
- Hibernate和Mybatis进行比较