HDU-1226 超级密码 (BFS+剪枝)
密
码是一个C进制的数,并且只能由给定的M个数字构成,同时密码是一个给定十进制整数N(0<=N<=5000)的正整数倍(如果存在多个满足
条件的数,那么最小的那个就是密码),如果这样的密码存在,那么当你输入它以后门将打开,如果不存在这样的密码......那就把门炸了吧.
注意:由于宝藏的历史久远,当时的系统最多只能保存500位密码.因此如果得到的密码长度大于500也不能用来开启房门,这种情况也被认为密码不存在.
入数据的第一行是一个整数T(1<=T<=300),表示测试数据的数量.每组测试数据的第一行是两个整数N(0<=N&
lt;=5000)和C(2<=C<=16),其中N表示的是题目描述中的给定十进制整数,C是密码的进制数.测试数据的第二行是一个整数
M(1<=M<=16),它表示构成密码的数字的数量,然后是M个数字用来表示构成密码的数字.两个测试数据之间会有一个空行隔开.
注意:在给出的M个数字中,如果存在超过10的数,我们约定用A来表示10,B来表示11,C来表示12,D来表示13,E来表示14,F来表示15.我保证输入数据都是合法的.
注意:构成密码的数字不一定全部都要用上;密码有可能非常长,不要试图用一个整型变量来保存密码;我保证密码最高位不为0(除非密码本身就是0).
# include<iostream>
# include<cstdio>
# include<string>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
string ans;
int val;
node(string s,int m):ans(s),val(m){}
bool operator < (const node &a) const {
if(ans.size()==a.ans.size())
return ans>a.ans;
return ans.size()>a.ans.size();
}
};
int vis[],n,m,c,num[];
int get()
{
char ch;
cin>>ch;
if(ch>=''&&ch<='')
return ch-'';
return ch-'A'+;
}
void bfs()
{
memset(vis,,sizeof(vis));
priority_queue<node>q;
for(int i=;i<m;++i){
if(num[i]==)
continue;
string s;
if(num[i]<=)
s+=num[i]+'';
else
s+=num[i]-+'A';
vis[num[i]%n]=;
q.push(node(s,num[i]%n));
}
while(!q.empty())
{
node u=q.top();
q.pop();
if(u.val==){
cout<<u.ans<<endl;
return ;
}
if(u.ans.size()>=)
continue;
for(int i=;i<m;++i){
int now=(u.val*c+num[i])%n;
if(vis[now])
continue;
string s=u.ans;
if(num[i]<=)
s+=num[i]+'';
else
s+=num[i]-+'A';
vis[now]=;
q.push(node(s,now));
}
}
printf("give me the bomb please\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&c);
scanf("%d",&m);
for(int i=;i<m;++i)
num[i]=get();
sort(num,num+m);
if(n==){
if(num[]==)
printf("0\n");
else
printf("give me the bomb please\n");
continue;
}
bfs();
}
return ;
}
TLE的DFS+剪枝写法:
# include<iostream>
# include<cstdio>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std;
int vis[],n,m,c,num[],flag;
string aans;
int get()
{
char ch;
cin>>ch;
if(ch>=''&&ch<='')
return ch-'';
return ch-'A'+;
}
bool is_smaller(string s1,string s2)
{
if(s1.size()<s2.size())
return true;
if(s1.size()==s2.size()&&s1<s2)
return true;
return false;
}
void dfs(string ans,int s)
{
if(ans.size()>)
return ;
if(s==){
if(!flag){
aans=ans;
flag=;
}
else{
if(is_smaller(ans,aans))
aans=ans;
}
return ;
}
//cout<<ans<<endl;
if(flag){
if(aans.size()<ans.size())
return ;
if(aans.size()==ans.size()&&ans>aans)
return ;
}
string temp=ans;
for(int i=;i<m;++i){
int ns=(s*c+num[i])%n;
if(vis[ns])
continue;
if(num[i]<=)
ans+=num[i]+'';
else
ans+=num[i]+'A'-;
vis[ns]=;
dfs(ans,ns);
vis[ns]=;
ans=temp;
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&c);
scanf("%d",&m);
for(int i=;i<m;++i)
num[i]=get();
sort(num,num+m);
flag=;
memset(vis,,sizeof(vis));
for(int i=;i<m;++i){
//cout<<num[i]<<endl;
string ans;
if(num[i]==)
continue;
if(num[i]<=)
ans+=num[i]+'';
else
ans+=num[i]-+'A';
if(num[i]%n==){
aans=ans;
flag=;
break;
}
vis[num[i]%n]=;
dfs(ans,num[i]%n);
vis[num[i]%n]=;
}
if(!flag)
printf("give me the bomb please\n");
else
cout<<aans<<endl;
}
return ;
}
HDU-1226 超级密码 (BFS+剪枝)的更多相关文章
- hdu.1226.超级密码(bfs)
超级密码 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU 1226 超级密码(BFS) (还需研究)
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Desc ...
- HDU 1226 超级密码(数学 bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1226 超级密码 Time Limit: 20000/10000 MS (Java/Others) ...
- hdu 1226 超级密码
超级密码 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem D ...
- hdu 1226 超级密码(bfs+余数判重)
题意:略过 分析:用m个数字组成一个能够被n整除的c进制数的最小值,实际上本题的关键就在于这个最小值上. 首先确定我们的思路是从小到大寻找.先查看一位数,即查看着m个数字是否能被n整除:若不能,就查 ...
- HDOJ 1226 超级密码(bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1226 思路分析:题目要求寻找一串长度不大于500的C进制的密码,且该密码需要为十进制数N的整数倍. & ...
- HDU 1226 超级密码 (搜素)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1226 题意简单,本来是一道很简单的搜素题目. 但是有两个bug: 1.M个整数可能有重复的. 2.N可 ...
- hdu1226超级密码 bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1226/ 题目大意是:寻找一个五百位之内的C进制密码,该密码是N的正整数倍,而且只能用给定的数构成密码,求这样的密 ...
- HDOJ 1226 超级密码
超级密码 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 超级密码(bfs)
超级密码 Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
随机推荐
- git必备命令
git status 查看git的状态git add <path>的形式把我们<path>添加到索引库中,<path>可以是文件也可以是目录.git add -u ...
- web前端----JavaScript的DOM(三)
一.JS中for循环遍历测试 for循环遍历有两种 第一种:是有条件的那种,例如 for(var i = 0;i<ele.length;i++){} 第二种:for (var i in l ...
- 计算概论(A)/基础编程练习1(8题)/5:鸡兔同笼
#include<stdio.h> int main() { // 鸡兔同笼中脚的总数:a < 32768 int a; scanf("%d", &a); ...
- MySQL Crash Course #03# Chapter 5. 6 排序. BETWEEN. IS NULL
索引 排序检索的数据 SQL 过滤 vs. 应用程序过滤 简单 Where 补充:大小写敏感. BETWEEN. IS NULL Sorting Retrieved Data mysql> SE ...
- ES6学习--函数剩余参数 (rest参数)
ES6 引入 rest 参数(形式为“...变量名”),用于获取函数的多余参数,这样就不需要使用arguments对象了.rest 参数搭配的变量是一个数组,该变量将多余的参数放入数组中.(可以拿到除 ...
- Python3 识别验证码(opencv-python)
Python3 识别验证码(opencv-python) 一.准备工作 使用opencv做图像处理,所以需要安装下面两个库: pip3 install opencv-python pip3 insta ...
- linux 添加 swap
1)在linux下,首先,查看内存和swap大小: [root@rhel6 usr]# free -m total used free sha ...
- ArcThemALL!5.1:解压、脱壳、压缩样样精通
原文链接:http://www.ithome.com/html/soft/57033.htm ArcThemALL!软件主要功能: 1.支持压缩和解压功能,支持常用的7z.zip.cab.iso.ra ...
- console.time测试代码块执行时间
console.time('计时器'); for (var i = 0; i < 1000; i++) { for (var j = 0; j < 1000; j++) {} } cons ...
- 解决方案:c调用python,PyImport_Import或者PyImport_ImportModule总是返回为空
下面c_python_utils.h是处理工具函数,test.cpp是测试程序,hello.py是python类 可是当我集成到项目中的时候,PyImport_Import总是返回为空,起初我以为是i ...