HDU - 1226 超级密码

题目连接

重点是余数判重 其他就是简单搜索问题


但是有两个bug:

1、M个整数可能有重复的。

2、N可能为0。 


int judge(node &a)  
{  
    int tmp=0;  
    int i;  
    for(i=0;i<a.len;i++)  
    {  
        tmp=(tmp*c+a.base[i])%n;  
    }  
    return tmp;  
}  



#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 30;
const int inf = 1<<30;
int n,c,m;
int mark[18];
bool h[18],vis[5005];
struct node
{
	int left,len;
	string s;
};
char int_char( int k )  
{  
    if( k <= 9 )  
        return k + '0';  
    return k-10 + 'A';  
} 
void BFS()
{
	node cur,cnt;
	queue<node>que;
	memset(vis,0,sizeof(vis));
	for( int i = 0; i < m; i ++ ){   
		if( mark[i] ){
			cur.left = mark[i]%n;            //大数取余
			cur.s = "";  cur.len = 1;
			cur.s += int_char(mark[i]);
			if( !cur.left ){
				cout<<cur.s<<endl;
				return;
			}
			if( !vis[cur.left] ){
				vis[cur.left] = 1;
				que.push(cur);
			}
		}
	}
	while( !que.empty() ){
		cur = que.front(); que.pop();
		if( cur.len >= 500 )
			break;
		for( int i = 0; i < m; i ++ ){
			cnt = cur;
			cnt.left = (cnt.left*c+mark[i])%n;
			if( vis[cnt.left] )
				continue;
			vis[cnt.left] = true;
			cnt.len ++;
			cnt.s += int_char(mark[i]);
			if( !cnt.left ){
				cout<<cnt.s<<endl;
				return;
			}
			que.push(cnt);
		}
	}
	puts("give me the bomb please");  
}
int main()
{
    //freopen("data.txt","r",stdin);   
	int cas;
	char ch[2];
	scanf("%d",&cas);
	while( cas -- ){
		scanf("%d%d%d",&n,&c,&m);
		memset(h,0,sizeof(h));
		for( int i = 0; i < m; i ++ ){
			scanf("%s",&ch);
			if( isdigit(ch[0]) )   //标记出现的数字
				h[ch[0]-'0'] = 1;
			else
				h[ch[0]-'A'+10] = 1;
		}
		m = 0;
		for( int i = 0; i < 16; i ++ ) //存入数组mark
			if( h[i] )
				mark[m++] = i;
		if( n != 0 ){
			BFS();
		}
		else{
			if( mark[0] == 0 )
				puts("0");
			else
				 puts("give me the bomb please"); 
		}
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值