HDU 4474 Yet Another Multiple Problem
2014-10-19 16:38
309 查看
精妙,涨姿势了!!
#include<stdio.h> #include<string.h> #include<queue> #define N 10100 using namespace std; int pre ,num ,a[10]; int n,m,x; void init() { memset(pre,-1,sizeof(pre)); memset(num,-1,sizeof(num)); memset(a,0,sizeof(a)); } void print(int u) { if (pre[u]!=-1) print(pre[u]); printf("pre[%d] = %d, num = %d\n",u, pre[u], num[u]); //其实就是一个数分解成单个数输出 } int bfs() { int i,j,t; queue<int>q; for (i=1;i<10;++i) { if (!a[i]) { t=i%n; if (t==0) { printf("%d",i);return 0;} q.push(t); num[t] = i; } } while (!q.empty()) { int u=q.front(); q.pop(); //printf("u = %d\t", u); for (i=0;i<10;++i) { if (!a[i]) { t=(10*u + i)%n;//这样做得到的数一定不含那m个数的任何一个,除模相当于剪枝, if (num[t]==-1) { q.push(t); pre[t]=u; num[t]=i; } if (t==0) { printf("# %d #", 10*u*i); print(t); return 0; } } } } printf("-1"); return 0; } int main () { //freopen("2.txt", "w", stdout); int i,j,k=0; while (scanf("%d%d",&n,&m)!=EOF) { init(); while (--m>=0) { scanf("%d",&x); a[x]=1; } printf("Case %d: ",++k); bfs(); printf("\n"); } return 0; }
相关文章推荐
- HDU 4474 Yet Another Multiple Problem(搜索剪枝)
- [bfs+余数判重+路径记录] hdu 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem【BFS+一个判断技巧】
- 【搜索】 HDU 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem(12年成都区域赛-K题-BFS)
- hdu 4474 Yet Another Multiple Problem(BFS)
- Hdu 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem
- Yet Another Multiple Problem (hdu 4474)
- hdu 4474 Yet Another Multiple Problem ( bfs + math)
- HDU 4474 Yet Another Multiple Problem 搜索
- HDU---4474 Yet Another Multiple Problem【广度优先搜索】
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
- HDU 4474 Yet Another Multiple Problem(搜索 BFS)
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索
- hdu 4474 Yet Another Multiple Problem (bfs)
- HDU 4474:Yet Another Multiple Problem
- HDU 4474(Yet Another Multiple Problem)(BFS+同余定理)