HDU-1664-Different Digits(BFS)
digits 1, 3 and 4.
7
15
16
101
0
7
555
16
1111
思路:最多仅仅须要出现两个不同的数字,先尝试一个数字的情况。再搜两个数字的情况。
#include <stdio.h>
#define min(A,B)(A<B?A:B)
#define INF 999999999 struct N{
int len,num;
bool operator<(const struct N &p) const
{
if(len==p.len) return num<p.num; return len<p.len;
}
}sin,tt; struct S{
int val,last,m,len;
}que[1000000],t; int i,j;
bool mod[65536];
char ans[100][1000]; void dfs(int idx)
{
if(que[idx].last!=-1) dfs(que[idx].last); ans[i*10+j][que[idx].len]=que[idx].val+'0';
} int main()
{
int n,k,temp,id,mnlen; while(~scanf("%d",&n) && n)
{
sin.len=INF;
sin.num=INF; for(i=1;i<=9;i++)
{
for(j=0;j<n;j++) mod[j]=0;
temp=0;
for(j=1;;j++)
{
temp=(temp*10+i)%n; if(!temp)
{
tt.len=j;
tt.num=i;
sin=min(sin,tt);
} if(!mod[temp]) mod[temp]=1;
else break;
}
} if(sin.len<INF)
{
for(i=0;i<sin.len;i++) printf("%d",sin.num);
puts(""); continue;
} if(n<100)
{
printf("%d\n",n); continue;
} mnlen=INF; for(i=1;i<=9;i++)
{
for(j=0;j<=9;j++)
{
if(i==j) continue; for(k=0;k<n;k++) mod[k]=0; que[0].val=i;
que[1].val=j;
que[0].last=-1;
que[1].last=0;
que[0].m=i;
que[1].m=i*10+j;
que[0].len=0;
que[1].len=1; mod[i]=mod[i*10+j]=1; int top=0;
int bottom=2; while(top<bottom)
{
t=que[top]; if(!t.m)
{
if(t.len<mnlen)
{
mnlen=t.len;
id=i*10+j;
}
dfs(top);
ans[i*10+j][t.len+1]=0;
break;
} if(i<j)
{
if(!mod[(t.m*10+i)%n])
{
mod[(t.m*10+i)%n]=1; que[bottom].last=top;
que[bottom].len=t.len+1;
que[bottom].m=(t.m*10+i)%n;
que[bottom++].val=i;
} if(!mod[(t.m*10+j)%n])
{
mod[(t.m*10+j)%n]=1; que[bottom].last=top;
que[bottom].len=t.len+1;
que[bottom].m=(t.m*10+j)%n;
que[bottom++].val=j;
}
}
else
{
if(!mod[(t.m*10+j)%n])
{
mod[(t.m*10+j)%n]=1; que[bottom].last=top;
que[bottom].len=t.len+1;
que[bottom].m=(t.m*10+j)%n;
que[bottom++].val=j;
} if(!mod[(t.m*10+i)%n])
{
mod[(t.m*10+i)%n]=1; que[bottom].last=top;
que[bottom].len=t.len+1;
que[bottom].m=(t.m*10+i)%n;
que[bottom++].val=i;
}
} top++;
}
}
} puts(ans[id]);
}
}
HDU-1664-Different Digits(BFS)的更多相关文章
- HDU 4333 Revolving Digits 扩张KMP
标题来源:HDU 4333 Revolving Digits 意甲冠军:求一个数字环路移动少于不同数量 等同 于的数字 思路:扩展KMP求出S[i..j]等于S[0..j-i]的最长前缀 推断 nex ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- hdu 2102 A计划-bfs
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- HDU 1072(记忆化BFS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...
- HDU 2364 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...
- HDU 2579 (记忆化BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2579 题目大意:走迷宫.对于障碍点,只有当前(dep+1)%k才能走,问最少时间. 解题思路: 只有 ...
- HDU 2653 (记忆化BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
- HDU 4707 Pet(BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数 Sample I ...
随机推荐
- JDK和JRE的差异和区别
来源:http://docs.oracle.com/javase/7/docs/
- Linux下Tomcat catalina.out自动归档,以及logrotate 配置详解
Linux下Tomcat catalina.out自动归档 如果 catalina.out 日志达到 2GB 大小的时候,Tomcat 因为缓存问题,便没有办法继续输出日志了. 为了避免这种情况,你 ...
- python中mcmc方法的实现
MCMC方法在贝叶斯统计中运用很多,MIT发布的EMCEE是实现的比较好的.介绍页面在下面.源代码中examples里的代码可以帮助理解各种功能,特别是line.py 列出了最小二乘法,最大似然法和M ...
- 第五节 Code 128 码
128码开始於1981年推出,是一种长度可变.连续性的字母数字条码.与其他一维条码比较起来,128码是较为复杂的条码系统,而其所能支援的字元也相对地比其他一维条码来得多,又有不同的编码方式可供交互运用 ...
- delphi数字签名验证及能够获取数字签名文件信息(利用wintrust.dll的导出函数,翻译一下)
unit TrustCheck; interface uses Windows,SysUtils,jwaWinTrust,JwaWinCrypt; function CheckFileTrust(co ...
- poj 2021 Relative Relatives(暴力)
题目链接:http://poj.org/problem?id=2021 思路分析:由于数据较小,采用O(N^2)的暴力算法,算出所有后代的年龄,再排序输出. 代码分析: #include <io ...
- android错误之android.content.res.Resources$NotFoundException:
错误:android.content.res.Resources$NotFoundException: String resource ID #0x1 原因:一般发生在参数 int resId 错误, ...
- 杭电 HDU 1242 Rescue
http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...
- Opencv2系列学习笔记2(图像的其它操作)
本节主要涉及到图像的领域.算术操作以及如何操作图像感兴趣的区域. 一:邻域操作 以下例子主要对图像进行锐化.基于拉普拉斯算子<后面讨论>.这幅图像的边缘部分将得到放大,细节部分将更加的锐利 ...
- QQ与我联系
第一种 <a href=" http://sighttp.qq.com/cgi-bin/check?sigkey=ee8bdb91c04a9ae912a305a5a2461a0d8d6 ...