hdu 4294(bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4294
思路:题目的意思是说,给你n,k,则求出n的正整数倍数,使得这个数字在k进制下表示的时候需要的不同数字最小。首先明确最多的不同数字需要2种,证明如下a,aa,aaa,aaaa,......找出n个连续的,那么中间至少有两个mod n的值是相等的,那么这两个数字相减,得到aaa...000肯定能被n整除,因此可以bfs搜索只含有1个数字的和只含有2个数字的。
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<string>
- using namespace std;
- #define MAXN 77777
- struct Node{
- int num,val,pre,len;
- }que[MAXN];
- int number[];
- int n,k,min_len,length,flag,End;
- string ans,tmp;
- bool mark[MAXN];
- bool bfs(int m)
- {
- Node now,next;
- memset(mark,false,sizeof(mark));
- int head=,tail=-;
- for(int i=;i<=m;i++)if(number[i]){
- now.len=;
- now.num=number[i];
- now.pre=-;
- now.val=number[i]%n;
- que[++tail]=now;
- mark[now.val]=true;
- }
- while(head<=tail){
- now=que[head];
- if(now.len>min_len)break;
- if(now.val==){
- End=head;
- length=now.len;
- return true;
- }
- for(int i=;i<=m;i++){
- next=now;
- next.val=(now.val*k+number[i])%n;
- if(!mark[next.val]){
- mark[next.val]=true;
- next.len++;
- next.num=number[i];
- next.pre=head;
- que[++tail]=next;
- }
- }
- head++;
- }
- return false;
- }
- void Get_String(int k)
- {
- if(k==-)return ;
- Get_String(que[k].pre);
- tmp+=(que[k].num+'');
- }
- int main()
- {
- while(~scanf("%d%d",&n,&k)){ //input整数n和k进制
- ans="";
- min_len=;
- flag=;
- for(int i=;i<k;i++){ //先搜索只有1个数字的
- number[]=i;
- if(bfs()){
- flag=;
- tmp="";
- Get_String(End);
- if(length<min_len||(length==min_len&&tmp<ans)){
- ans=tmp;
- min_len=length;
- }
- }
- }
- if(flag){
- cout<<ans<<endl;
- continue;
- }
- for(int i=;i<k;i++){ //搜索2个数字的
- number[]=i;
- for(int j=i+;j<k;j++){
- number[]=j;
- if(bfs()){
- tmp="";
- Get_String(End);
- if(length<min_len||(length==min_len&&tmp<ans)){
- ans=tmp;
- min_len=length;
- }
- }
- }
- }
- cout<<ans<<endl;
- }
- return ;
- }
hdu 4294(bfs)的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 5025 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...
- HDU 1429 (BFS+记忆化状压搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1429 题目大意:最短时间内出迷宫,可以走回头路,迷宫内有不同的门,对应不同的钥匙. 解题思路: 要是 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
随机推荐
- v-for设置键值 key
总是用 key 配合 v-for.在组件上_总是_必须用 key 配合 v-for,以便维护内部组件及其子树的状态. <ul> <li v-for="todo in tod ...
- java String->float,float->int
类型转换代码 : String sourceStr = "0.0"; String类型 float sourceF = Float.valueOf(sourceStr); floa ...
- MVC 强类型传值Model。和弱类型传值ViewData[""]。及用EF进行增删查改(母版页的使用)
<1> 控制器 </pre><pre name="code" class="csharp">using MvcTest.Mo ...
- Android自动化测试之MonkeyRunner录制和回放脚本
Android自动化测试之MonkeyRunner录制和回放脚本(十一) 分类: 自动化测试 Android自动化 2013-02-22 10:57 7346人阅读 评论(2) 收藏 举报 andro ...
- Git-在一个电脑上同时使用两个Git的账号
前言 又需要登录公司的账号,又想在电脑上使用自己的账号. 实现 首先是git config方面的设置,要取消掉原本对于git账号的全局设置. git config --global --unset u ...
- iperf使用
1. sourceforge搜索iperf下载 2. ./configure make make install 3. server:iperf -s -p 12345 -i 1 -M: client ...
- NoSQL(三)
redis集群介绍 1.官方叫cluster,redis3.0才直接的一个架构,如果数据量很大,单台机器已经无法满足存储,查询的瓶颈,所以我们需要多台机器构成一个大集群,用来解决存储空间,查询速度,负 ...
- shared-service.ts
shared-service.ts import { Observable } from 'rxjs/Observable'; import { Injectable } from '@angular ...
- DDR 复位
将FPGA代码和实际的数字电路对应起来. always @ (negedge clk_ref_200) begin if(ddr3_init_done) 'b10) 'b10 ...
- Docker使用Dockerfile创建支持ssh服务自启动的容器镜像
原文链接:Docker使用Dockerfile创建支持ssh服务自启动的容器镜像 1. 首先创建一个Dockerfile文件.文件内容例如以下 # 选择一个已有的os镜像作为基础 FROM cento ...