USACO sec2.1 Hamming Codes
直接枚举就行了,不带回溯的搜索。
/* PROG : hamming LANG : C++ */ # include <stdio.h> # define id(x) ((x)>>3) # define of(x) ((x)&0x7) # define get(x) ((h[id(x)]>>of(x))&0x1) # define set(x) (h[id(x)] |= (0x1<<of(x))) int n, b, d; char h[(1<<5) + 1]; int sol[70]; /*************************************************/ int dis(int x, int y) { int s = x ^ y, r = 0; while (s) { ++r; s &= s-1; } return r; } void search(int cur, int cnt) { int i, j; if (cnt > n) return ; for (i = cur+1; i < (1<<b); ++i) if (!get(i)) { for (j = 1; j <= cnt; ++j) if (dis(sol[j], i) < d) break; set(i); if (j > cnt) {sol[cnt+1] = i; search(i, cnt+1);} } } /*************************************************/ void solve(void) { int i; scanf("%d%d%d", &n, &b, &d); sol[1] = 0, set(0); search(0, 1); for (i = 1; i <= n; ++i) { if (i%10 != 1) putchar(' '); printf("%d", sol[i]); if (i<n && i%10 == 0) putchar('\n'); } putchar('\n'); } int main() { freopen("hamming.in", "r", stdin); freopen("hamming.out", "w", stdout); solve(); fclose(stdin); fclose(stdout); return 0; }