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 ...
随机推荐
- G - RPG的错排(错排)
Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R是公主,P是草儿 ...
- 分布式session
前端用户请求经过随机分发之后,可能会命中后端任意的Web Server,并且 Web Server 也可能会因为各种不确定的原因宕机.在这种情况下,session 是很难在集群间同步的,而通过将ses ...
- JavaScript时钟实例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- [LeetCode]题解(python):044-Wildcard Matching
题目来源: https://leetcode.com/problems/wildcard-matching/ 题意分析: 定义两个新字符规则,'?'代表任意一个字符,’*‘代表任意长度的任意字符.输入 ...
- Chukwa
http://baidutech.blog.51cto.com/4114344/748261/ http://blog.csdn.net/cnbird2008/article/details/1451 ...
- Python转码问题的解决方法:ignore,replace,xmlcharrefreplace
比如,若要将某个String对象s从gbk内码转换为UTF-8,可以如下操作 s.decode('gbk').encode('utf-8′) 可是,在实际开发中,我发现,这种办法经常会出现异常: Un ...
- uva11021 - Tribles(概率)
11021 - Tribles GRAVITATION, n.“The tendency of all bodies to approach one another with a strengthpr ...
- SICP 习题 (2.7) 解题总结 : 定义区间数据结构
SICP 习题 2.7 開始属于扩展练习,能够考虑不做,对后面的学习没什么影响.只是,假设上面的使用过程表示序对,还有丘奇计数你都能够理解的话,完毕这些扩展练习事实上没什么问题. 习题2.7是要求我们 ...
- STL之顺序容器
顺序容器: vector:数组 list:链表 deque:双端数组 顺序容器适配器: stack:堆栈 queue:队列 priority_queue:优先级队列 deque是一个动态数组 dequ ...
- for循环语句之求和,阶乘,求偶,求n次篮球蹦起高度
for循环语句格式: ;;/*循环条件*/i++/*状态改变*/) { //循环体,执行代码:(break;跳出循环体) } for 穷举法用循环把各种可能的情况都走一遍,然后用if条件把满足要求的结 ...