USACO 2.1 Hamming Codes

落日映苍穹つ 2021-12-23 08:47 232阅读 0赞

Problem 94: Hamming Codes
Rob Kolstad

Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of length B bits (1 <= B <= 8), such that each of the codewords is at least Hamming distance of D (1 <= D <= 7) away from each of the other codewords. The Hamming distance between a pair of codewords is the number of binary bits that differ in their binary notation. Consider the two codewords 0x554 and 0x234 and their differences (0x554 means the hexadecimal number with hex digits 5, 5, and 4):

  1. 0x554 = 0101 0101 0100
  2. 0x234 = 0010 0011 0100
  3. Bit differences: xxx xx

Since five bits were different, the Hamming distance is 5.

PROGRAM NAME: hamming

INPUT FORMAT

N, B, D on a single line

SAMPLE INPUT (file hamming.in)

  1. 16 7 3

OUTPUT FORMAT

N codewords, sorted, in decimal, ten per line. In the case of multiple solutions, your program should output the solution which, if interpreted as a base 2^B integer, would have the least value.

SAMPLE OUTPUT (file hamming.out)

  1. 0 7 25 30 42 45 51 52 75 7682 85 97 102 120 127
  2. Type:DFS
  3. 思路:先计算出0-2^B-1,任意两个数的距离,可以动态的求解。再根据:0必然在最后的结果中,因为如果最小的数是b0
  4. 让每个数减去b0,仍然符合距离的限制条件。所以只需从0开始按递增的顺序搜索,一旦搜到答案,就可exit;

ContractedBlock.gif ExpandedBlockStart.gif 代码

#include < stdio.h >
#include < stdlib.h >
#include < iostream.h >
#define MAX (1 << 8 + 1)
#define NMAX 65
#define BMAX 10
#define DMAX 10

int nums[NMAX], dist[MAX][MAX];
int N, B, D, maxval;
FILE * in , * out ;

void findgroups( int cur, int start) {
int a, b, canuse;
char ch;
if (cur == N) {
for (a = 0 ; a < cur; a ++ ) {
if (a % 10 )
fprintf( out , “ “ );
fprintf( out , “ %d “ , nums[a]);
if (a % 10 == 9 || a == cur - 1 )
fprintf( out , “ \n “ );
}
exit( 0 );
}
for (a = start; a < maxval; a ++ ) {
canuse = 1 ;
for (b = 0 ; b < cur; b ++ )
if (dist[nums[b]][a] < D) {
canuse = 0 ;
break ;
}
if (canuse) {
nums[cur] = a;
findgroups(cur + 1 , a + 1 );
}
}
}

int main() {
in = fopen( “ hamming.in “ , “ r “ );
out = fopen( “ hamming.out “ , “ w “ );
int a, b, c;
fscanf( in , “ %d%d%d “ , & N, & B, & D);
maxval = ( 1 << B);
for (a = 0 ; a < maxval; a ++ )
for (b = 0 ; b < maxval; b ++ ) {
dist[a][b] = 0 ;
for (c = 0 ; c < B; c ++ )
if ((( 1 << c) & a) != (( 1 << c) & b))
dist[a][b] ++ ;
}
nums[ 0 ] = 0 ;
findgroups( 1 , 1 );
return 0 ;
}

转载于:https://www.cnblogs.com/superbin/archive/2010/06/03/1750977.html

发表评论

表情:
评论列表 (有 0 条评论,232人围观)

还没有评论,来说两句吧...

相关阅读

    相关 matlab hamming code

    Hamming code 是一种用于检测和纠正错误的纠错码,在 Matlab 中也可以实现它的编码和译码过程。 具体的实现方法可以通过自定义函数实现,例如实现编码和译码的过程

    相关 Visual Studio Code v1.21发布

    欢迎阅读2018年2月发布的Visual Studio代码。本版本中有许多重要更新,我们希望您会喜欢,其中一些主要亮点包括: [新的通知用户界面][Link 1] -