Created
March 11, 2010 12:00
-
-
Save begoon/329063 to your computer and use it in GitHub Desktop.
Problem about removal pears from the bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
/** | |
* There 16 pears in the bucket 4x4. How to remove 6 pears to guarantee | |
* even numbers of pears in each row and column. | |
*/ | |
int b[16]; | |
int f() { | |
int a; | |
if (b[0] + b[1] + b[ 2] + b[ 3] + b[ 4] + b[ 5] + b[ 6] + b[ 7] + | |
b[8] + b[9] + b[10] + b[11] + b[12] + b[13] + b[14] + b[15] != 10) | |
return 0; | |
if ((b[ 0] + b[ 1] + b[ 2] + b[ 3]) & 1) return 0; | |
if ((b[ 4] + b[ 5] + b[ 6] + b[ 7]) & 1) return 0; | |
if ((b[ 8] + b[ 9] + b[10] + b[11]) & 1) return 0; | |
if ((b[12] + b[13] + b[14] + b[15]) & 1) return 0; | |
if ((b[ 0] + b[ 4] + b[ 8] + b[12]) & 1) return 0; | |
if ((b[ 1] + b[ 5] + b[ 9] + b[13]) & 1) return 0; | |
if ((b[ 2] + b[ 6] + b[10] + b[14]) & 1) return 0; | |
if ((b[ 3] + b[ 7] + b[11] + b[15]) & 1) return 0; | |
return 1; | |
} | |
int main() { | |
int i; | |
int c = 0; | |
for (i = 0; i < (1 << 16); ++i) { | |
int j; | |
for (j = 0; j < 16; ++j) b[j] = (i & (1 << j)) ? 1 : 0; | |
if (!f()) continue; | |
printf("#%d:\n", ++c); | |
for (j = 0; j < 16; ++j) { | |
printf("%d ", b[j]); | |
if ((j & 0x03) == 0x03) printf("\n"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment