#include <stdio.h>
#define DEBUG 1
#define TESTCASES 9
int codewords, numOfBits, hammingDist;
int max;
int result[257];
int countBit1(int num){
int numOfBit1 = 0;
while (num){
num &= (num - 1);
numOfBit1++;
}
return numOfBit1;
}
int findCodeword(int count){
if (count == codewords){
int i;
for (i = 0; i < count; i++)
printf("%d%c", result[i], i % 10 == 9 ? '\n' : ' ');
printf("\n");
return 1;
}
int num;
for (num = result[count - 1] + 1; num < max; num++){
int ok = 1;
int i;
for (i = 0; i < count; i++){
if (countBit1(num ^ result[i]) < hammingDist){
ok = 0;
break;
}
}
if (ok){
result[count] = num;
if (findCodeword(count + 1))
return 1;
}
}
}
int main(){
#if DEBUG
int testCase;
for (testCase = 1; testCase <= TESTCASES; testCase++){
char inputFileName[20] = "inputX.txt";
inputFileName[5] = '1' + (testCase - 1);
freopen(inputFileName, "r", stdin);
printf("\n#%d\n", testCase);
#endif
scanf("%d%d%d", &codewords, &numOfBits, &hammingDist);
max = 1 << numOfBits;
findCodeword(1);
#if DEBUG
}
#endif
return 0;
}
USACO 2.1 Hamming Codes (递归)
最新推荐文章于 2020-06-24 16:03:13 发布