标签:des style blog class code c
Description
Input
Output
Sample Input
3 22 10 3 7 0 1 2 10 1 1 25 16 3 A B C
Sample Output
110 give me the bomb please CCB
思路:BFS而已,但是做了好久,结束的 条件就是cur%n==0,其中m个数又可以产生新的数,也要记录
#include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 5005; queue<int> q; int n,c,d[22],pre[MAXN],val[MAXN],cnt[MAXN]; int vis[MAXN]; int bfs(int deep){ memset(vis, 0, sizeof(vis)); while (!q.empty()) q.pop(); for (int i = 0; i < deep; i++) if (d[i]){ int cur = d[i]%n; if (!vis[cur]){ vis[cur] = 1; val[cur] = d[i]; cnt[cur] = 1; pre[cur] = -1; if (cur == 0) return 1; q.push(cur); } } while (!q.empty()){ int cur = q.front(); q.pop(); if (cnt[cur] == 500) return 0; for (int i = 0; i < deep; i++){ int to = (cur*c+d[i])%n; if (!vis[to]){ vis[to] = 1; val[to] = d[i]; pre[to] = cur; cnt[to] = cnt[cur]+1; if (to == 0) return 1; q.push(to); } } } return 0; } void print(int cur){ if (cur == -1) return ; print(pre[cur]); if (val[cur] >= 10) printf("%c", val[cur]-10+‘A‘); else printf("%d", val[cur]); } int main(){ int t, m; scanf("%d", &t); while (t--){ scanf("%d%d", &n, &c); scanf("%d", &m); for (int i = 0; i < m; i++){ char ch[3]; scanf("%s", ch); if (ch[0] >= ‘0‘ && ch[0] <= ‘9‘) d[i] = ch[0]-‘0‘; else d[i] = ch[0]-‘A‘+10; } sort(d, d+m); int k = 1; for (int i = 1; i < m; i++) if(d[i] != d[i-1]) d[k++] = d[i]; if (n == 0){ if (d[0] == 0) printf("0\n"); else printf("give me the bomb please\n"); } else { if (!bfs(k)) printf("give me the bomb please"); else print(0); printf("\n"); } } return 0; }
HDU - 1226 超级密码,布布扣,bubuko.com
标签:des style blog class code c
原文地址:http://blog.csdn.net/u011345136/article/details/25554901