码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1226 搜索水题

时间:2014-10-13 12:22:19      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:搜索

题目就不多说了    密码共为500位   完完全全的广搜       密码其中一个条件是n的倍数  所以最多只可能出现4999中状态     广搜到底         每一位1-15  

代码比较简单

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;


int n,m,c,mark[20],visit[5100];
struct node
{
int len;
int prt[510];
}a,b;
int judge(node s)
{
int i,t=0;
for(i=1;i<=s.len;i++)
{
t=(t*c+s.prt[i])%n;
}
return t;
}
int print(node s)
{
for(int i=1;i<=s.len;i++)
{
if(s.prt[i]<=9) printf("%d",s.prt[i]);
else printf("%c",s.prt[i]-10+‘A‘);
}
printf("\n");
return 0;
}
int bfs()
{
int i,j,x=1;
memset(visit,0,sizeof(visit));
a.len=0;
a.prt[0]=0;
queue<node>q;
q.push(a);
while(!q.empty())
{
b=q.front();
q.pop();
for(i=0;i<16;i++)
{
if(!mark[i]) continue;
if(x==1&&i==0) continue;//第一位不能使零
a=b;
a.len=b.len+1;
a.prt[a.len]=i;
int k=judge(a);
if(k==0)
{
print(a);
return 1;
}
if(visit[k]) continue;
if(a.len<=499);
{
visit[k]=1;
q.push(a);
}
}
x++;
}
return 0;
}
int main()
{
int  T,i,j;
char str[15];
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&c);
scanf("%d",&m);
memset(mark,0,sizeof(mark));
for(i=1;i<=m;i++)
{
scanf("%s",str);
if(str[0]>=‘0‘&&str[0]<=‘9‘)
mark[str[0]-‘0‘]=1;
else 
mark[str[0]-‘A‘+10]=1;
}
if(n==0)
{
if(mark[0]) printf("0\n");
else printf("give me the bomb please\n");
}
else
{
int flash=bfs();
if(!flash) printf("give me the bomb please\n");
}
}
return 0;
}   

hdu1226 搜索水题

标签:搜索

原文地址:http://blog.csdn.net/zxf654073270/article/details/40026341

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!