HDU 1226 BFS

注意密码位数<=500 输出注意十六进制改成字母

要点题目都已说明 ac就好

 

#include<iostream>

#include<stdio.h>

#include<string.h>

#include<algorithm>

#include<queue>



#define N 5100

#define L(x) (x<<1)

#define R(x) (x<<1|1)

using namespace std;



inline int Max(int a,int b){return a>b?a:b;}

inline int Min(int a,int b){return a<b?a:b;}

int a[20],k,n,c;

int vis[N]; //取模后出现的可能只会是 [0, n-1]

int pre[N];

int num[N];

void BFS(){

	memset(vis, 0, sizeof(vis));

	memset(pre, -1, sizeof(pre));

	queue<int>q;

	int mod;



	for(int i=0;i<k;i++)

	{

		if(!a[i])continue;

		mod = a[i]%n;

		if(!vis[mod])

		{

			vis[mod] = 1;

			num[mod] = a[i];

			q.push(mod);			

		}



		if(a[i]%n == 0)

		{if(a[i]<=10)printf("%d\n",a[i]); else printf("%c\n",a[i]-10+'A');return ;}

	}

	bool su =false;

	while(!q.empty() && su == false){

		mod = q.front(); q.pop();

		for(int i=0;i<k;i++)

		{

			int now = (mod*c + a[i])%n;

			if(now == 0){ pre[now] = mod;num[now]=a[i]; su = true; break; }

			if(!vis[now])

			{

				q.push(now);				

				vis[now]=1;

				pre[now] = mod;

				num[now] = a[i];

			}

		}

	}



	if(su == false){printf("give me the bomb please\n");return ;}

	int top=0,ans[N];

	for(int i=0; i!=-1;i=pre[i])

		ans[top++]=num[i];

	if(top>500){printf("give me the bomb please\n");return ;}

	for(int i=top-1;i>=0;i--)

		if(ans[i]>=10)

			printf("%c",ans[i] - 10 +'A');

		else 

			printf("%d",ans[i]);

	printf("\n");

}



int main(){

	int T;scanf("%d",&T);



	while(T--){

		scanf("%d %d %d",&n,&c,&k);



		for(int i=0;i<k;i++){

			char c=getchar();

			while( !( '0'<=c && c<='9') && !( 'A'<=c && c<='F')) c=getchar();



			if( '0'<=c && c<='9' ) a[i] = c - '0';

			else a[i] = c - 'A' +10;

		}



		sort(a,a+k);



		if(n == 0){

			if(a[0])printf("give me the bomb please\n");

			else printf("0\n");

			continue;

		}



		BFS();

	}

	return 0;

}

/*

3

22 10

3

7 0 1



2 10

1

1



25 16

3

A B C



*/


 

 

你可能感兴趣的:(HDU)