summaryrefslogtreecommitdiff
path: root/2.1/hamming.c
blob: abc3483ca8a4f53c70f3627b8c95add362f5e21e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
ID: mytbk921
LANG: C
TASK: hamming
*/

#include <stdio.h>

typedef unsigned char u8;

static unsigned
popcount(unsigned x)
{
	unsigned c = 0;
	while (x) {
		if (x&1) c++;
		x >>= 1;
	}
	return c;
}

static unsigned
hamdist(unsigned x, unsigned y)
{
	return popcount(x^y);
}

u8 s[64];
int N, B, D;

int search(int i)
{
	int t, j;
	if (i==N) return 1;

	for (t=s[i-1]+1; t<(1<<B); t++) {
		for (j=0; j<i; j++) {
			if (hamdist(s[j], t)<D)
				break;
		}
		if (j==i) {
			s[j] = t;
			if (search(i+1))
				return 1;
		}
	}
	/* all searched */
	return 0;
}

int main()
{
	int i;

	FILE *fin = fopen("hamming.in", "r");
	FILE *fout = fopen("hamming.out", "w");
	fscanf(fin, "%d%d%d", &N, &B, &D);
	fclose(fin);

	s[0] = 0;
	if (search(1)) {
		for (i=0; i<N; i++) {
			if (i%10==0) {
				if (i>0)
					fprintf(fout, "\n");
			} else {
				fprintf(fout, " ");
			}
			fprintf(fout, "%d", s[i]);
		}
		fprintf(fout, "\n");
	}
	fclose(fout);
	return 0;
}