HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
2013-10-09 21:15
246 查看
原文链接:https://www.geek-share.com/detail/2592324500.html
没什么巧办法,直接搜就行。
用余数作为每个节点的哈希值。
#include <cstdio> #include <cstring> #include <cstdlib> const int MAXN = 10100; struct node { int mod; int fa; int digit; node() {} node( int mod, int fa, int dig ):mod(mod), fa(fa), digit(dig) { } }; int N; bool ok[12]; bool vis[MAXN]; node Q[MAXN << 2]; void findFa( int u ) { if ( u == -1 ) return; findFa( Q[u].fa ); printf( "%d", Q[u].digit ); } int BFS() { int head = 0; int tail = 0; memset( vis, false, sizeof(vis) ); for ( int i = 1; i < 10; ++i ) { if ( !ok[i] ) continue; int hashh = i % N; if ( vis[hashh] ) continue; vis[hashh] = true; Q[tail] = node( hashh, -1, i ); if ( hashh == 0 ) return tail; ++tail; } while ( head < tail ) { node cur = Q[head]; for ( int i = 0; i < 10; ++i ) { if ( !ok[i] ) continue; int hashh = ( cur.mod * 10 + i ) % N; if ( vis[hashh] ) continue; vis[hashh] = true; Q[tail] = node( hashh, head, i ); if ( hashh == 0 ) return tail; ++tail; } ++head; } return -1; } int main() { int cas = 0; while ( scanf( "%d", &N ) == 1 ) { memset( ok, true, sizeof(ok) ); int n; scanf( "%d", &n ); for ( int i = 0; i < n; ++i ) { int digit; scanf( "%d", &digit ); ok[digit] = false; } printf( "Case %d: ", ++cas ); int ans = BFS(); if ( ans != -1 ) findFa( ans ); else printf("-1"); puts(""); } return 0; }
转载于:https://www.cnblogs.com/GBRgbr/p/3360143.html
相关文章推荐
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
- HDOJ 4474 Yet Another Multiple Problem(BFS + 剪枝)
- hdu 4474 Yet Another Multiple Problem bfs
- HDU 4474 HDOJ Yet Another Multiple Problem 2012ACM亚洲赛成都赛区K题
- 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(BFS)
- 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
- hdu 4474 Yet Another Multiple Problem 模型转换 BFS搜索
- hdu 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem (bfs+mod运算)
- HDU 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
- HDU 4474 Yet Another Multiple Problem
- hdu 4474 Yet Another Multiple Problem(按位枚举,4级)
- HDU 4474 Yet Another Multiple Problem